#include<iostream>
using namespace std;
int main ()
{
    long long n, br=0;
    cin>>n;
    long long a[n+10];
    long long b[n+10];

    for (long long i=1; i<=n; i++)
    {
        cin>>a[i];
        if (i>2)
        {
            if ((a[i-1]<a[i] && a[i-1]<a[i-2]) || (a[i-1]>a[i] && a[i-1]>a[i-2])) {b[i-1]=1; br++; //cout<<"DUNER"<<endl;}
            else {b[i-1]=0; //cout<<"PIZZA"<<endl;}
        }
    }

    if (br==0)
    {
        for (long long i=1; i<=n; i++)
        {
            br=br+(n-i);
            //cout<<"pizza"<<endl;
        }
        cout<<br<<endl;
    }
    else
    {
        br=n-1;
        for (long long i=1; i<n-1; i++)
        {
            for (long long j=i+2; j<=n; j++)
            {
                if (b[j-1]!=1) {br++;}
                else
                {
                    break;
                }
            }

        }
        cout<<br*2<<endl;
    }

    return 0;
}
