#include <iostream>
using namespace std;

int main() {

    int N, h1, h2, h3, c = 0, curr = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        h1 = h2;
        h2 = h3;
        cin >> h3;
        if (i > 1) {
            if ((h2 > h1 && h2 > h3) || (h2 < h1 && h2 < h3)) curr = 0;
            else {
                curr++;
                c += curr;
            }
        }
    }

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

    return 0;

}

