#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    long long n,a,br = 0;
    cin >> n;
    stack<pair<long long,long long>> haha;
    stack<pair<long long,long long>> bruh;
    for(long long i = 0; i < n; i++) {
        cin >> a;
        while(!haha.empty() && haha.top().first < a) {
            haha.pop();
            bruh.pop();
        }
        if(haha.empty()) {
            br+=i;
            haha.push({a,i});
            bruh.push({-1,0});
        }
        else {
            if(haha.top().first == a) {
                br+=i-bruh.top().first-1;
                br+=bruh.top().second; /// !!!!!!!!!!!!!!!!
                haha.push({a,i});
                bruh.push(bruh.top());
            }
            else {
                br+=i-haha.top().second-1;
                br+=haha.size();
                bruh.push({haha.top().second,haha.size()});
                haha.push({a,i});
            }
        }
    }
    cout << br;
    return 0;
}
