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<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
int b[210000];
int c[210000];
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a;i++)scanf("%d",b+i);
	for(int i=0;i<a;i++)c[b[i]-1]=i;
	long long ret=0;
	set<int>S;
	S.insert(-1);
	S.insert(a);
	for(int i=0;i<a;i++){
		int left=c[i]-(*(--(S.lower_bound(c[i]))));
		int right=(*(S.upper_bound(c[i])))-c[i];
		ret+=(long long)(i+1)*left*right;
	//	printf("%d %d\n",left,right);
		S.insert(c[i]);
	}
	printf("%lld\n",ret);
}