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