#include<iostream>
using namespace std;
int main()
{
    long long broiMesta;
    cin>>broiMesta;
    long long mesta[broiMesta],im, brDolini=0, brVurhove=0;
    for(im=0; im<broiMesta; im++){
        cin>>mesta[im];
        if(im>0){
            if(mesta[im-2]>mesta[im-1]&&mesta[im]>mesta[im-1]){
                brDolini++;
            }else{
                if(mesta[im-2]<mesta[im-1]&&mesta[im]<mesta[im-1]){
                    brVurhove++;
                }
            }
        }
    }
    cout<<broiMesta*(broiMesta-1)/2-(brDolini+brVurhove)/2<<endl;
    return 0;
}
