#include<bits/stdc++.h>
using namespace std;
vector<long long> now[1000001];
vector<long long> l8r[1000001];
int main(){
    long long n;
    cin>>n;
    for(long long i=0;i<n;i++){
        long long x;
        cin>>x;
        now[0].push_back(x);
    }
    long long branchesNow=1,branchesL8r=0;
    long long ans=0;
    while(branchesNow!=0){
        for(long long i=0;i<branchesNow;i++){
            long long Max=0,MaxInd;
            for(long long j=0;j<now[i].size();j++){
                if(Max<now[i][j]){
                    Max=now[i][j];
                    MaxInd=j;
                }
            }
            long long maxesInARow=0;
            for(long long j=MaxInd;now[i][j]==now[i][MaxInd] && j<now[i].size();j++){
                maxesInARow++;
            }
            ans+=(now[i].size()-maxesInARow)*maxesInARow+maxesInARow*(maxesInARow-1)/2;
            //cout<<long long(now[i].size())*maxesInARow-maxesInARow+maxesInARow*(maxesInARow-1)/2<<" ";
            //cout<<now[i].size()<<" "<<maxesInARow<<"\n";
            for(long long j=0;j<MaxInd;j++){
                l8r[branchesL8r].push_back(now[i][j]);
            }
            if(l8r[branchesL8r].size()!=0){
                branchesL8r++;
            }
            for(long long j=MaxInd+maxesInARow;j<now[i].size();j++){
                l8r[branchesL8r].push_back(now[i][j]);
            }
            if(l8r[branchesL8r].size()!=0){
                branchesL8r++;
            }
        }
        for(long long i=0;i<branchesL8r;i++){
            /*for(long long j=0;j<l8r[i].size();j++){
                cout<<l8r[i][j]<<" ";
            }
            cout<<"\n";*/
            now[i]=l8r[i];
            l8r[i].clear();
        }
        //cout<<"\n";
        branchesNow=branchesL8r;
        branchesL8r=0;
    }
    cout<<ans;
}
