#include <cstdio>
#include <vector>
using namespace std;

long long doit(long long x) {
	return (x+1)*x / 2;
}

int main() {
	int N;
	scanf("%d", &N);
	vector<int> V(N);
	for (int &x : V) {
		scanf("%d", &x);
	}

	long long ans = 0;
	for (int i = 0; i < N;) {
		// find the largest possible j such that [i, j) is weird
		int lastJ = i+1;
		for (int j = i+1; j < N; j++) {
			if (V[j] == V[j-1]) break;
			if (j >= i+2) {
				int inc1 = V[j-1] > V[j-2];
				int inc2 = V[j] > V[j-1];
				if (inc1 == inc2) break;
			}
			lastJ++;
		}
		ans += doit(lastJ - i);
		if (lastJ == N) break;
		if (V[lastJ] == V[lastJ-1]) i = lastJ;
		else if (lastJ == i + 1) i = lastJ;
		else {
			i = lastJ - 1;
			ans--;
		}
	}

	printf("%lld\n", ans);

	return 0;
}
