#include<stdio.h>
#define LL long long
const LL inf  = 1e18;
int que[100010];
long long  height[100010];
int main()
{

    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int top = 0;
        que[top] = n + 1;
        height[n + 1] =  inf;
        long long ans = 0;
        for(int i = 1;i <= n;i++) scanf("%lld",&height[i]);

        for(int i = n; i >= 1; i--)
        {
            while(top >= 0 && height[que[top]] < height[i]) top--;
            ans = ans + que[top] - i - 1;
            top++;
            que[top] = i;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

/**************************************************************
	Problem: 2032
	User: admin
	Language: C
	Result: Accepted
	Time:127 ms
	Memory:2316 kb
****************************************************************/