#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,br1;
    cin >> n;
    stack<pair<long long,long long>> haha;
    stack<long long> bruh;
    for(long long i = 0; i < n; i++) {
        cin >> a;
        br1 = br;
        while(!haha.empty() && haha.top().first < a) {
            haha.pop();
            bruh.pop();
        }
        if(haha.empty()) {
            br+=i;
            haha.push({a,i});
            bruh.push(-1);
        }
        else {
            if(haha.top().first == a) {
                br+=i-bruh.top()-1;
                br+=haha.size()-1;
                haha.push({a,i});
                bruh.push({bruh.top()});
            }
            else {
                br+=i-haha.top().second-1;
                br+=haha.size();
                bruh.push(haha.top().second);
                haha.push({a,i});
            }
        }
    }
    cout << br;
    return 0;
}
