import java.io.*;
import java.util.*;

public class solution {

    public static long compute(long x) {
        return (x+1)*x/2;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [] V = new int[N];
        for (int i = 0; i < N; i++) {
            V[i] = sc.nextInt();
        }
        
        long ans = 0;
        for (int i = 0; i < N;) {
            int lastJ = i+1;
            for (int j = i+1; j < N; j++) {
                if (V[j] == V[j-1]) break;
                if (j >= i+2) {
                    boolean inc1 = V[j-1] > V[j-2];
                    boolean inc2 = V[j] > V[j-1];
                    if (inc1 == inc2) break;
                }
                lastJ++;
            }
            ans += compute(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--;
            }
        }
        System.out.println(ans);
    }
}
