import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



"""

・大きい数から挿入していく

・連結成分の両端を管理する

"""



N,*A = list(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)
import sys

import numpy as np



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



def maximal_rectangle(H):

    """ヒストグラムに対して、極大長方形を計算。

    各 i に対して、[l,r) が高さ H[i] となるような l, r を計算する。

    アリ本 p.299～p.300

    """

    N = len(H)

    L = np.empty(N, np.int32)

    R = np.empty(N, np.int32)

    st = np.zeros(N, np.int32)

    # 左端の計算

    t = 0

    for i in range(N):

        while t and H[st[t - 1]] >= H[i]:

            t -= 1

        L[i] = 0 if t == 0 else st[t - 1] + 1

        st[t] = i

        t += 1

    # 右端の計算

    t = 0

    for i in range(N - 1, -1, -1):

        while t and H[st[t - 1]] >= H[i]:

            t -= 1

        R[i] = N if t == 0 else st[t - 1]

        st[t] = i

        t += 1

    return L, R



signature = '(i8[:],)'

if sys.argv[-1] == 'ONLINE_JUDGE':

    from numba.pycc import CC

    cc = CC('my_module')

    cc.export('maximal_rectangle', signature)(maximal_rectangle)

    cc.compile()



from my_module import maximal_rectangle



A = np.array(read().split(), np.int64)[1:]



N = len(A)

L, R = maximal_rectangle(A)

coef = (np.arange(N) - L + 1) * (R - np.arange(N))

x = np.sum(coef * A)

print(x)
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
"""
・大きい数から挿入していく
・連結成分の両端を管理する
"""
N, *A = list(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)

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def maximal_rectangle(H):
    """ヒストグラムに対して、極大長方形を計算。
    各 i に対して、[l,r) が高さ H[i] となるような l, r を計算する。
    アリ本 p.299～p.300
    """
    N = len(H)
    L = np.empty(N, np.int32)
    R = np.empty(N, np.int32)
    st = np.zeros(N, np.int32)
    # 左端の計算
    t = 0
    for i in range(N):
        while t and H[st[t - 1]] >= H[i]:
            t -= 1
        L[i] = 0 if t == 0 else st[t - 1] + 1
        st[t] = i
        t += 1
    # 右端の計算
    t = 0
    for i in range(N - 1, -1, -1):
        while t and H[st[t - 1]] >= H[i]:
            t -= 1
        R[i] = N if t == 0 else st[t - 1]
        st[t] = i
        t += 1
    return L, R


signature = "(i8[:],)"
if sys.argv[-1] == "ONLINE_JUDGE":
    from numba.pycc import CC

    cc = CC("my_module")
    cc.export("maximal_rectangle", signature)(maximal_rectangle)
    cc.compile()
from my_module import maximal_rectangle

A = np.array(read().split(), np.int64)[1:]
N = len(A)
L, R = maximal_rectangle(A)
coef = (np.arange(N) - L + 1) * (R - np.arange(N))
x = np.sum(coef * A)
print(x)

+import numpy as np -""" -・大きい数から挿入していく -・連結成分の両端を管理する -""" -N, *A = list(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) + + +def maximal_rectangle(H): +    """ヒストグラムに対して、極大長方形を計算。 +    各 i に対して、[l,r) が高さ H[i] となるような l, r を計算する。 +    アリ本 p.299～p.300 +    """ +    N = len(H) +    L = np.empty(N, np.int32) +    R = np.empty(N, np.int32) +    st = np.zeros(N, np.int32) +    # 左端の計算 +    t = 0 +    for i in range(N): +        while t and H[st[t - 1]] >= H[i]: +            t -= 1 +        L[i] = 0 if t == 0 else st[t - 1] + 1 +        st[t] = i +        t += 1 +    # 右端の計算 +    t = 0 +    for i in range(N - 1, -1, -1): +        while t and H[st[t - 1]] >= H[i]: +            t -= 1 +        R[i] = N if t == 0 else st[t - 1] +        st[t] = i +        t += 1 +    return L, R + + +signature = "(i8[:],)" +if sys.argv[-1] == "ONLINE_JUDGE": +    from numba.pycc import CC + +    cc = CC("my_module") +    cc.export("maximal_rectangle", signature)(maximal_rectangle) +    cc.compile() +from my_module import maximal_rectangle + +A = np.array(read().split(), np.int64)[1:] +N = len(A) +L, R = maximal_rectangle(A) +coef = (np.arange(N) - L + 1) * (R - np.arange(N)) +x = np.sum(coef * A) +print(x)