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
class SparseTable():
    def __init__(self, array, n):
        '''テーブルを構築する'''
        self.row_size = n.bit_length()

        # log_tableを構築する
        # log_table = [0, 0, 1, 1, 2, 2, 2, 2, ...]
        self.log_table = [0] * (n + 1)
        for i in range(2, n + 1):
            self.log_table[i] = self.log_table[i//2] + 1

        # sparse_tableを構築する
        self.sparse_table = [[0] * n for _ in range(self.row_size)]
        for i in range(n):
            self.sparse_table[0][i] = array[i]
        for row in range(1, self.row_size):
            for i in range(n - (1 << row) + 1):
               self.sparse_table[row][i] = self._merge(self.sparse_table[row - 1][i], \
                                                       self.sparse_table[row - 1][i + (1 << row - 1)])

    def _merge(self, num1, num2):
        '''クエリの内容'''
        return min(num1, num2)

    def query(self, l, r):
        '''区間[l, r)に対するクエリに答える'''
        if r == l:
            return None
        row = self.log_table[r - l]
        return self._merge(self.sparse_table[row][l], self.sparse_table[row][r - (1 << row)])
  
n = int(input())
a = list(map(int, input().split()))
st = SparseTable(a, n)
ans = 0
for i in range(n):
    ok = i + 1
    ng = n + 1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if st.query(ok, mid) < a[i]:
            ng = mid
        else:
            ok = mid
    tmp1 = ok - i - 1
    
    ok = i
    ng = -1
    mid = (ok + ng) // 2
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if st.query(mid, i + 1) < a[i]:
            ng = mid
        else:
            ok = mid
    tmp2 = i - ok
    ans += ((tmp1+1) * (tmp2+1))* a[i]
print(ans)