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
#!/usr/bin/env python3
#AGC5 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
lst = [0 for i in range(n+1)]
for i in range(n):
    lst[a[i]] = i

l = [0]*n
r = [0]*n
ans = 0
for i in range(1,n+1)[::-1]:
    x = lst[i]
    if x + 1 < n and a[x+1] > i:
        R = r[x+1]
    else:
        R = x
    if x - 1 >= 0 and a[x-1] > i:
        L = l[x-1]
    else:
        L = x
    l[R] = L
    r[L] = R
    ans += (x - L + 1)*(R - x + 1)*i
print(ans)