Alice has the following permutation A of integers from 1 to N, (a1,a2,...,an). She wants to find sum of minimum values over all subarrays of A. i.e. find ∑l=Nl=1∑r=Nr=lmin(al,al+1,...,ar)
Input:
First line contains an integer N, the size of array A.
The second line consists of N space seperated integers denoting (a1,a2,...,an)
(a1,a2,...,an) is a permutation of (1,2,3..,.n)
Output:
In a single line, output the answer.
Constraints:
1≤N≤5∗105
1≤ai≤N
Sample Tescases:
Input:
3
1 3 2
Output:
10
Explanation:
Answer=min(a1)+min(a2)+min(a3)+min(a1,a2)+min(a2,a3)+min(a1,a2,a3)
=1+3+2+1+2+1
=10