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

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; i++) {
		// 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 += lastJ - i;
	}

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

	return 0;
}
