#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];

    stack <int> temp;
    int64_t ans = 0;
    for (int i = 0; i < n; i++)
    {
        while (!temp.empty() && a[i] > a[temp.top()])
        {
            biggerOnRight[temp.top()] = i;
            temp.pop();
        }
        temp.push(i);
    }
    while (!temp.empty()){biggerOnRight[temp.top()] = -1; temp.pop();}

    for (int i = 0; i < n; i++)
    {
        //cout << biggerOnRight[i] << " " << i << endl;
        if (biggerOnRight[i] == -1)
        {
            ans += n-i-1;
            continue;
        }

        else
            ans += biggerOnRight[i] - i;

        int j = biggerOnRight[i];
        while (biggerOnRight[j] != -1)
        {
            j = biggerOnRight[j];
            ans++;
        }
    }
    cout << ans << endl;

    return 0;
}
