/*
#include <stdio.h>
#include <string.h>
int stack[1000]={};
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
char a[150]={}, b[150]={}, c=0;
int topa=0, topb=0, numa=0, numb=0, num=0;
scanf("%s %s", a, b);
topa=strlen(a)-1;
topb=strlen(b)-1;
while(1)
{
numa=numb=0;
if(topa!=-1)
{
numa=a[topa--]-'0';
}
if(topb!=-1)
{
numb=b[topb--]-'0';
}
num=numa+numb+c;
push(num%10);
c=num/10;
if(topa==-1&&topb==-1)
{
if(c!=0)
{
push(num/10);
}
break;
}
}
while(top!=-1)
{
printf("%d", pop());
}
return 0;
}
6 9 7 6 4 6
6
7
9
*/
/*
#include <stdio.h>
int stack[1000000]={};
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
int n, h;
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d", &h);
while(top!=-1 && stack[top]<=h)
{
pop();
}
push(h);
}
printf("%d", top+1);
return 0;
}
*/
#include <stdio.h>
long long int stack[80000];
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
int n, a[80001]={};
scanf("%d", &n);
long long int sum=0;
for(int i=0; i<n; i++)
{
scanf("%lld", &a[i]);
if(top==-1)
{
push(n);
}
else if(a[top]<=a[i])
{
while(top!=-1&&(a[top]<=a[i]))
{
pop();
}
}
top++;
stack[top]=a[i];
sum+=top;
}
printf("%d", sum);
return 0;
}/*
#include <stdio.h>
#include <string.h>
int stack[1000]={};
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
char a[150]={}, b[150]={}, c=0;
int topa=0, topb=0, numa=0, numb=0, num=0;
scanf("%s %s", a, b);
topa=strlen(a)-1;
topb=strlen(b)-1;
while(1)
{
numa=numb=0;
if(topa!=-1)
{
numa=a[topa--]-'0';
}
if(topb!=-1)
{
numb=b[topb--]-'0';
}
num=numa+numb+c;
push(num%10);
c=num/10;
if(topa==-1&&topb==-1)
{
if(c!=0)
{
push(num/10);
}
break;
}
}
while(top!=-1)
{
printf("%d", pop());
}
return 0;
}
6 9 7 6 4 6
6
7
9
*/
/*
#include <stdio.h>
int stack[1000000]={};
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
int n, h;
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d", &h);
while(top!=-1 && stack[top]<=h)
{
pop();
}
push(h);
}
printf("%d", top+1);
return 0;
}
*/
#include <stdio.h>
long long int stack[80000];
int top=-1;
void push(int x)
{
top++;
stack[top]=x;
}
int pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int main()
{
int n, a[80001]={};
scanf("%d", &n);
long long int sum=0;
for(int i=0; i<n; i++)
{
scanf("%lld", &a[i]);
if(top==-1)
{
push(n);
}
else if(a[top]<=a[i])
{
while(top!=-1&&(a[top]<=a[i]))
{
pop();
}
}
top++;
stack[top]=a[i];
sum+=top;
}
printf("%d", sum);
return 0;
}