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>
using namespace std;
const int MAXN=200010;

int n,v[MAXN],e[MAXN],d[MAXN];
long long int resp;

int main ()
{
	scanf("%d",&n);

	for(int i=1;i<=n;i++)
		scanf("%d",&v[i]);

	v[0]=-1,	v[n+1]=-1;

	for(int i=1;i<=n;i++)
	{
		int cur=i-1;
		while(v[cur]>v[i])	cur=e[cur];
		e[i]=cur;
	}

	for(int i=n;i>0;i--)
	{
		int cur=i+1;
		while(v[cur]>v[i])	cur=d[cur];
		d[i]=cur;
	}

	for(int i=1;i<=n;i++)
	{
		long long int r1=d[i]-i-1;
		long long int r2=i-e[i]-1;
		resp+=(r1*r2+r1+r2+1)*v[i];
	}

	printf("%lld\n",resp);
}