#include <bits/stdc++.h>
#define endl '\n'

using namespace std;

constexpr int MAXN = 2*1e6 + 100;
int a[MAXN];
int biggerOnRight[MAXN];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
        cin >> a[i];

    int64_t answer = ((n)*(n-1))/2;
    deque <int> dq;

    for (int i = 0; i < n; i++)
    {
        while (!dq.empty() && a[i] >= a[dq.back()])
            dq.pop_back();

        if (!dq.empty())
        {
            answer = answer - dq.front();
            //cout << a[dq.front()] << endl;
        }


        dq.push_back(i);
    }

    cout << answer-1 << endl;

    return 0;
}
