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
n = int(input())
A = list(map(int, input().split()))

P = [0]*n
for i, a in enumerate(A):
    P[a-1] = i

import heapq
q = []
heapq.heapify(q)
R = [n-1]*n
for i, a in enumerate(A):
    while q and q[0]*(-1) > a:
        v = heapq.heappop(q)*(-1)
        R[v-1] = i-1
    heapq.heappush(q, a*(-1))

q = []
L = [0]*n
for j in range(n):
    i = n-1-j
    a = A[i]
    while q and q[0]*(-1) > a:
        v = heapq.heappop(q)*(-1)
        L[v-1] = i+1
    heapq.heappush(q, a*(-1))

#print(L)
#print(R)
ans = 0
for i in range(n):
    a = i+1
    l, r, p = L[i], R[i], P[i]
    ans += (p-l+1)*(r-p+1)*a
print(ans)