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