code = r"""

# distutils: language=c++

from libcpp.set cimport set

from libcpp.vector cimport vector

from libcpp.iterator cimport iterator

from cython.operator cimport dereference as deref, predecrement as dec

cdef int n = int(input()), i

cdef long c

cdef iterator it, jt

cdef vector[long] a = [*map(int, input().split())]

cdef vector[long] l = [0] * n

for i in range(n): l[a[i] - 1] = i

cdef set[long] s = set[long]([-1, n])

for i in range(n):

    c += (deref(s.lower_bound(l[i])) - l[i]) * (l[i] - deref(dec(s.lower_bound(l[i])))) * (i + 1)

    s.insert(l[i])

print(c)

"""

import os, sys

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

    open('solve.pyx', 'w').write(code)

    os.system('cythonize -i -3 -b solve.pyx')

import solve
code = r"""

# distutils: language=c++

from libcpp.set cimport set

from libcpp.vector cimport vector

from cython.operator cimport dereference as deref, predecrement as dec

cdef int n = int(input()), i

cdef long c

cdef vector[long] a = [*map(int, input().split())]

cdef vector[long] l = [0] * n

for i in range(n): l[a[i] - 1] = i

cdef set[long] s = set[long]([-1, n])

cdef set[long].iterator it

for i in range(n):

    it = s.lower_bound(l[i])

    c += (deref(it) - l[i]) * (l[i] - deref(dec(it))) * (i + 1)

    s.insert(l[i])

print(c)

"""

import os, sys

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

    open('solve.pyx', 'w').write(code)

    os.system('cythonize -i -3 -b solve.pyx')

import solve
code = r"""
# distutils: language=c++
from libcpp.set cimport set
from libcpp.vector cimport vector
from libcpp.iterator cimport iterator
from cython.operator cimport dereference as deref, predecrement as dec
cdef int n = int(input()), i
cdef long c
cdef iterator it, jt
cdef vector[long] a = [*map(int, input().split())]
cdef vector[long] l = [0] * n
for i in range(n): l[a[i] - 1] = i
cdef set[long] s = set[long]([-1, n])
for i in range(n):
    c += (deref(s.lower_bound(l[i])) - l[i]) * (l[i] - deref(dec(s.lower_bound(l[i])))) * (i + 1)
    s.insert(l[i])
print(c)
"""
import os, sys

if sys.argv[-1] == "ONLINE_JUDGE":
    open("solve.pyx", "w").write(code)
    os.system("cythonize -i -3 -b solve.pyx")
import solve

code = r"""
# distutils: language=c++
from libcpp.set cimport set
from libcpp.vector cimport vector
from cython.operator cimport dereference as deref, predecrement as dec
cdef int n = int(input()), i
cdef long c
cdef vector[long] a = [*map(int, input().split())]
cdef vector[long] l = [0] * n
for i in range(n): l[a[i] - 1] = i
cdef set[long] s = set[long]([-1, n])
cdef set[long].iterator it
for i in range(n):
    it = s.lower_bound(l[i])
    c += (deref(it) - l[i]) * (l[i] - deref(dec(it))) * (i + 1)
    s.insert(l[i])
print(c)
"""
import os, sys

if sys.argv[-1] == "ONLINE_JUDGE":
    open("solve.pyx", "w").write(code)
    os.system("cythonize -i -3 -b solve.pyx")
import solve

-from libcpp.iterator cimport iterator -cdef iterator it, jt +cdef set[long].iterator it -    c += (deref(s.lower_bound(l[i])) - l[i]) * (l[i] - deref(dec(s.lower_bound(l[i])))) * (i + 1) +    it = s.lower_bound(l[i]) +    c += (deref(it) - l[i]) * (l[i] - deref(dec(it))) * (i + 1)