//单调栈。。。
#include <iostream>
#include <stack>#include <cstdio>
using namespace std;
stack<long long> p;
int main()
{
long long n;
long long num;
while(cin >> n)
{
long long sum = 0;
cin >> num;
p.push(num);
for(int i = 2; i <= n; i++)
{
cin >> num;
while( !p.empty() && p.top() <= num) //注意等号啊
{
p.pop();
}
sum += p.size();
p.push(num);
}
printf("%I64d\n",sum);
while( !p.empty() )
p.pop();
}
return 0;
}