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 decimal
#decimal.getcontext().prec==500 Floatの桁数を500まで増やす
#Decimal(c).sqrt() それで平方根を求める
#Decimal(1) / Decimal(7) 1/7
"""
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys"""
#input = sys.stdin.readline


#N=int(input())

N=int(input())
A=list(map(int,input().split()))

lst=[[A[0],0,1]]
count=0

for i in range(1,N):
    m=1
    while lst and lst[-1][0]>A[i]:
        a,b,c=lst.pop()
        count+=a*(i-b)*c
        m+=c
    lst.append([A[i],i,m])
while lst:
    a,b,c=lst.pop()
    count+=a*(N-b)*c

print(count)
