#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #define M 80010 using namespace std; int n,top; int h[M],stack[M]; long long ans; void solve(){ for(int i=1;i<=n;i++){ while(top>0&&h[i]>=stack[top-1]) top--; ans+=top; stack[top]=h[i]; top++; } } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&h[i]); solve(); printf("%lld\n",ans); return 0; }