#include<iostream>
#include<queue>
using namespace std;
int st[80001],qe[80001];
int main()
{
int n,q;
long long I=0;
cin>>n;
int top1=0,top2=0;
queue<int> qu;
for(int i=1;i<=n;i++)
{
cin>>q;
qu.push(q);
}
st[++top1]=qu.front();
qe[++top2]=0;
qu.pop();
while(!qu.empty())
{
if(st[top1]>qu.front())
{
st[++top1]=qu.front();
qu.pop();
for(int i=1;i<=top2;i++)
qe[i]++;
qe[++top2]=0;
}
else
{
while(st[top1]<qu.front())
{
I+=qe[top2--];
top1--;
}
st[++top1]=qu.front();
qu.pop();
for(int i=1;i<=top2;i++)
qe[i]++;
qe[++top2]=0;
}
}
if(top1==1)
cout<<I;
else
cout<<I+1;
return 0;
}