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
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

"""
・大きい数から挿入していく
・連結成分の両端を管理する
"""

N,*A = map(int,read().split())

A = [0] + A + [0]
ind = [0] * (N+1)
for i,x in enumerate(A):
    ind[x] = i

left = list(range(1,len(A)+1))
right = list(range(-1,len(A)-1))
answer = 0
for i in ind[:0:-1]:
    # i番目に数を挿入
    l = left[i-1]; r = right[i+1]
    left[r] = l; right[l] = r
    x = i - l + 1; y = r - i + 1
    answer += A[i] * (x * y)

print(answer)