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

using namespace std;

constexpr int MAXN = 2*1e6 + 100;
int a[MAXN];
int biggestOnRight[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];

    int MAX = a[n-1];
    int MAXINDEX = n-1;

    biggestOnRight[n-1] = -1;

    for (int i = n-1; i >= 0; i--)
    {
        if (a[i] >= MAX)
        {
            biggestOnRight[i] = -1;

            if (a[i] > MAX)
            {
                MAX = a[i];
                MAXINDEX = i;
            }
        }

        else
            biggestOnRight[i] = MAXINDEX;

    }

    int64_t ans = 0;
    for (int i = 0; i < n-1; i++)
    {
        if (biggestOnRight[i] == -1)
            ans += (n-i-1)*1LL;
        else
            ans += (biggestOnRight[i]-i)*1LL;

        //cout << i+1 << " " << biggestOnRight[i]+1 << endl;
    }

    cout << ans << endl;

    return 0;
}
