One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

<image>

Constraints

* 1 ≦ N ≦ 200,000
* (a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:


N
a_1 a_2 ... a_N


Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Examples

Input

3
2 1 3


Output

9


Input

4
1 3 2 4


Output

19


Input

8
5 4 8 1 2 6 7 3


Output

85
#include<bits/stdc++.h>
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rep2(i,k,n) for(int i=k;i>=n;i--)
using namespace std;
const int N=2e5+7;
int n,a[N],l[N],r[N],stk[N],top;
long long ans=0;
int main(){
	scanf("%d",&n);rep(i,1,n)scanf("%d",&a[i]);
	top=0;
	rep(i,1,n){
		while(top && a[stk[top]] > a[i])top--;
		l[i]=stk[top]+1;
		stk[++top]=i;
	}
	top=0;
	rep2(i,n,1){
		while(top && a[stk[top]] > a[i])top--;
		if(!top)r[i]=n;
		else r[i]=stk[top]-1;
		stk[++top]=i;
	}
	rep(i,1,n){
		ans+=1ll*(i-l[i]+1)*(r[i]-i+1)*a[i];
	}
	printf("%lld\n",ans);
}