#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
long long int arr[n];
int top = -1;
long long int sum = 0;
for(int i=0;i<n;i++){
scanf("%lld", &arr[i]);
if(top==-1){
top++;
arr[top] = arr[i];
continue;
}
else if(arr[top]<=arr[i]){
while(top!=-1&&(arr[top]<=arr[i])){
arr[top] = 0;
top--;
}
}
top++;
arr[top] = arr[i];
sum += top;
}
printf("%d",sum);
}



