#include <iostream>
#include <utility>
using namespace std;

long long n,a[2000000],br;
pair<long long,pair<long long,long long>> p[2000000];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>> n;
    for(long long i=0;i<n;i++){
        cin>> a[i];
        if(i==0) p[i]={a[i],{i,1}};
        else{
            if(a[i]>p[i-1].first) p[i]={a[i],{i,1}};
            else if(a[i]<p[i-1].first) p[i]=p[i-1];
            else p[i]={a[i],{p[i-1].second.first,p[i-1].second.second+1}};
        }
    }
    for(long long i=n-1;i>=0;i--){
        if(p[i].first>a[i]){
            br+=p[i].second.second;
            if(p[i].second.first!=i-1) br++;
            for(long long i=n-2;i>p[i].second.first;i--){
                br++;
            }
            //cout<< p[i].second.first << " " << i << " " << p[i].second.second << " :: ";
        }else{
            br+=i;
            //cout<< "i=" << i <<" ";
        }
        //cout<< br << endl;
    }
    cout<< br;






    return 0;
}

