#include<bits/stdc++.h>
#define int long long
using namespace std;
stack<pair<int,int> > t;
int n,h,ans,s;
signed main(){
cin >> n;
while(n--){
s=1;
cin >> h;
while(!t.empty()){
if(t.top().first<=h){
ans+=t.top().second;
if(t.top().first==h)s+=t.top().second;
t.pop();
}else{
ans+=s;
break;
}
}
t.push(make_pair(h,s));
}
cout << ans;
return 0;
}