#include <iostream>
using namespace std;

int s[100000];

int main() {

    int N, h1, h2, h3, c = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        h1 = h2;
        h2 = h3;
        cin >> h3;
        if (i > 1) {
            if (h2 > h1 && h2 > h3) s[i - 1] = 1;
            else if (h2 < h1 && h2 < h3) s[i - 1] = 2;
            else s[i - 1] = 0;
        }
    }

    for (int i = 0; i < N - 2; i++) {
        for (int j = i + 2; j < N; j++) {
            if (s[j - 1]) break;
            else c++;
        }
    }

    cout << 2 * (c + N - 1);

    return 0;

}
