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"

int n,ptr;
int a[200005];
long long sum,d;
int stV[200005];
int stC[200005];


int main(){
	scanf("%d",&n);
	for(int i = 0; i < n; i++){
		scanf("%d",&a[i]);
		int c = 1;
		while(ptr>0&&stV[ptr-1]>a[i]){
			d -= stV[ptr-1]*(long long)stC[ptr-1];
			c += stC[ptr-1];
			ptr--;
		}

		d += a[i]*(long long)c;
		stC[ptr] = c;
		stV[ptr] = a[i];
		ptr++;

		sum += d;
	}
	printf("%lld\n",sum);
}