import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 7)



# 大きい数字から追加していく。そのときの連結成分の大きさだけ足される。

# union findっぽく。各成分の左端を根として持つ



N = int(eval(input()))

A = [int(x) for x in input().split()]



x_to_i = {x:i for i,x in enumerate(A)}



V = set()

root = dict()

size = dict()



def find_root(x):

    y = root[x]

    if x == y:

        return y

    ry = find_root(y)

    root[x] = ry

    return ry



def merge(x,y):

    rx = find_root(x)

    ry = find_root(y)

    sx = size[rx]

    sy = size[ry]

    if sx > sy:

        rx,ry = ry,rx

        sx,sy = sy,sx

    root[rx] = ry

    size[ry] += sx



answer = 0

for x in range(N,0,-1):

    i = x_to_i[x]

    V.add(i)

    size[i] = 1

    root[i] = i

    left = 0

    right = 0

    if i-1 in V:

        left = size[find_root(i-1)]

        merge(i-1,i)

    if i+1 in V:

        right = size[find_root(i+1)]

        merge(i+1,i)

    cnt = (left+1) * (right+1)

    answer += x * cnt



print(answer)
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

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
# 大きい数字から追加していく。そのときの連結成分の大きさだけ足される。
# union findっぽく。各成分の左端を根として持つ
N = int(eval(input()))
A = [int(x) for x in input().split()]
x_to_i = {x: i for i, x in enumerate(A)}
V = set()
root = dict()
size = dict()


def find_root(x):
    y = root[x]
    if x == y:
        return y
    ry = find_root(y)
    root[x] = ry
    return ry


def merge(x, y):
    rx = find_root(x)
    ry = find_root(y)
    sx = size[rx]
    sy = size[ry]
    if sx > sy:
        rx, ry = ry, rx
        sx, sy = sy, sx
    root[rx] = ry
    size[ry] += sx


answer = 0
for x in range(N, 0, -1):
    i = x_to_i[x]
    V.add(i)
    size[i] = 1
    root[i] = i
    left = 0
    right = 0
    if i - 1 in V:
        left = size[find_root(i - 1)]
        merge(i - 1, i)
    if i + 1 in V:
        right = size[find_root(i + 1)]
        merge(i + 1, i)
    cnt = (left + 1) * (right + 1)
    answer += x * cnt
print(answer)

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)

-input = sys.stdin.readline -sys.setrecursionlimit(10**7) -# 大きい数字から追加していく。そのときの連結成分の大きさだけ足される。 -# union findっぽく。各成分の左端を根として持つ -N = int(eval(input())) -A = [int(x) for x in input().split()] -x_to_i = {x: i for i, x in enumerate(A)} -V = set() -root = dict() -size = dict() - - -def find_root(x): -    y = root[x] -    if x == y: -        return y -    ry = find_root(y) -    root[x] = ry -    return ry - - -def merge(x, y): -    rx = find_root(x) -    ry = find_root(y) -    sx = size[rx] -    sy = size[ry] -    if sx > sy: -        rx, ry = ry, rx -        sx, sy = sy, sx -    root[rx] = ry -    size[ry] += sx - - +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)) -for x in range(N, 0, -1): -    i = x_to_i[x] -    V.add(i) -    size[i] = 1 -    root[i] = i -    left = 0 -    right = 0 -    if i - 1 in V: -        left = size[find_root(i - 1)] -        merge(i - 1, i) -    if i + 1 in V: -        right = size[find_root(i + 1)] -        merge(i + 1, i) -    cnt = (left + 1) * (right + 1) -    answer += x * cnt +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)