#include#include #include #include #define Maxn 100010using namespace std;__int64 ans=0;int Stack[Maxn],top;int main(){ int n,i,j,a; while(scanf("%d",&n)!=EOF) { ans=0; top=0; for(i=1;i<=n;i++) { scanf("%d",&a); while(top>0&&Stack[top]<=a) top--; ans+=(__int64)top; Stack[++top]=a; } printf("%I64d\n",ans); } return 0;}