#include<bits/stdc++.h>
using namespace std;
int n,h;
long long s;
struct node
{
int h,cnt;
};
stack<node> st;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>h;
if(!st.empty()&&st.top().h==h)
{
st.top().cnt++;
continue;
}
while(!st.empty()&&st.top().h<h)
{
node u=st.top();
st.pop();
s+=u.cnt*(u.cnt-1)/2+u.cnt;
if(!st.empty())
s+=u.cnt;
}
if(!st.empty()&&st.top().h==h)
st.top().cnt++;
else
st.push(node{h,1});
}
while(!st.empty())
{
node u=st.top();
st.pop();
s+=u.cnt*(u.cnt-1)/2;
if(!st.empty())
s+=u.cnt;
}
cout<<s;
return 0;
}