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
import sys

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

    import os, zlib, base64

    open('solve.pyx', 'wb').write(zlib.decompress(base64.b85decode("c${rg?P`QD5d7~`ER<5McXC316zM&QJxYwLw=|bXvi9iPcVn!*mIj3E%<Q-yxr3ep&rBvXV3ZwaHR4g1Wj1(s0YjahrYazVrpx3!gWP{{AE?>+jZWc@q6*xT+vpKlpOxRi<jL}obo!Ie&Q@+huWr41^f<_8HG&E$jG5S@_IN?d3D%kJaR4!tRGSxI-AJa%K+eW=JJ4{1!-8JVWDx2kiKi24txfXx@btV<Gzj|whXy*2>V3nbn}$cI4^Tq4z#e(_7H;aXQ50+10VUKpJANzDu-XWnK#BAg$Ze4gG7S<V@Uj{dqyg2Y9`{h6Bx7}zt*kadz%9z#eZ2#FD*mV{;&s-<|NLZWhB!J^#$D0Bb#AtOu38eS)O>k_eA!4&9IIJ48OI`$IB~L^$QKJkGN?M`hl*@K&#Ps64f(^p>0j-GY^un<0PhaYas")))

    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

import sys

if sys.argv[-1] == "ONLINE_JUDGE":
    import os, zlib, base64

    open("solve.pyx", "wb").write(
        zlib.decompress(
            base64.b85decode(
                "c${rg?P`QD5d7~`ER<5McXC316zM&QJxYwLw=|bXvi9iPcVn!*mIj3E%<Q-yxr3ep&rBvXV3ZwaHR4g1Wj1(s0YjahrYazVrpx3!gWP{{AE?>+jZWc@q6*xT+vpKlpOxRi<jL}obo!Ie&Q@+huWr41^f<_8HG&E$jG5S@_IN?d3D%kJaR4!tRGSxI-AJa%K+eW=JJ4{1!-8JVWDx2kiKi24txfXx@btV<Gzj|whXy*2>V3nbn}$cI4^Tq4z#e(_7H;aXQ50+10VUKpJANzDu-XWnK#BAg$Ze4gG7S<V@Uj{dqyg2Y9`{h6Bx7}zt*kadz%9z#eZ2#FD*mV{;&s-<|NLZWhB!J^#$D0Bb#AtOu38eS)O>k_eA!4&9IIJ48OI`$IB~L^$QKJkGN?M`hl*@K&#Ps64f(^p>0j-GY^un<0PhaYas"
            )
        )
    )
    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 +import sys -    open("solve.pyx", "w").write(code) +    import os, zlib, base64 + +    open("solve.pyx", "wb").write( +        zlib.decompress( +            base64.b85decode( +                "c${rg?P`QD5d7~`ER<5McXC316zM&QJxYwLw=|bXvi9iPcVn!*mIj3E%<Q-yxr3ep&rBvXV3ZwaHR4g1Wj1(s0YjahrYazVrpx3!gWP{{AE?>+jZWc@q6*xT+vpKlpOxRi<jL}obo!Ie&Q@+huWr41^f<_8HG&E$jG5S@_IN?d3D%kJaR4!tRGSxI-AJa%K+eW=JJ4{1!-8JVWDx2kiKi24txfXx@btV<Gzj|whXy*2>V3nbn}$cI4^Tq4z#e(_7H;aXQ50+10VUKpJANzDu-XWnK#BAg$Ze4gG7S<V@Uj{dqyg2Y9`{h6Bx7}zt*kadz%9z#eZ2#FD*mV{;&s-<|NLZWhB!J^#$D0Bb#AtOu38eS)O>k_eA!4&9IIJ48OI`$IB~L^$QKJkGN?M`hl*@K&#Ps64f(^p>0j-GY^un<0PhaYas" +            ) +        ) +    )