from bisect import*

class SqrtSet:

    def __init__(self,block_limit=201):

        self.key=[]

        self.child=[[]]

        self.block_limit=block_limit

    def search_lower(self,key):

        if not key:return None

        ret=None

        i=bisect_left(self.key,key)

        if i:ret=self.key[i-1]

        block=self.child[i]

        i=bisect_left(block,key)

        if i:ret=block[i-1]

        return ret

    def search_higher(self,key):

        if not key:return None

        ret=None

        i=bisect(self.key,key)

        if i<len(self.key):ret=self.key[i]

        block=self.child[i]

        i=bisect(block,key)

        if i<len(block):ret=block[i]

        return ret

    def insert(self,key):

        i=bisect(self.key,key)

        block=self.child[i]

        insort(block,key)

        if len(block)==self.block_limit:

            sep=self.block_limit//2

            self.key.insert(i,block[sep])

            self.child.insert(i+1,block[sep+1:])

            self.child[i]=block[:sep]

def main():

    n,*a=list(map(int,open(0).read().split()))

    l=[0]*n

    for i,v in enumerate(a,1):l[v-1]=i

    t=SqrtSet()

    t.insert(0)

    t.insert(n+1)

    c=0

    for i,v in enumerate(l,1):

        c+=(t.search_higher(v)-v)*(v-t.search_lower(v))*i

        t.insert(v)

    print(c)

main()
class UnionFind():

    def __init__(self,size):

        self.table=[-1]*size

    def root(self,x):

        while self.table[x]>=0:

            x=self.table[x]

        return x

    def unite(self,x,y):

        s1=self.root(x)

        s2=self.root(y)

        if s1!=s2:

            if self.table[s1]>self.table[s2]:

                s1,s2=s2,s1

            self.table[s1]+=self.table[s2]

            self.table[s2]=s1

        return

    def same(self,x,y):

        return self.root(x)==self.root(y)

    def size(self,x):

        return -self.table[self.root(x)]

n,*a=list(map(int,open(0).read().split()))

u=UnionFind(n)

b=[0]*n

for i,t in enumerate(a):b[t-1]=i

c=0

for k in b[::-1]:

    l=r=1

    if k>0 and a[k-1]>a[k]:

        l+=u.size(k-1)

        u.unite(k-1,k)

    if k+1<n and a[k+1]>a[k]:

        r+=u.size(k+1)

        u.unite(k+1,k)

    c+=l*r*a[k]

print(c)
from bisect import *


class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit

    def search_lower(self, key):
        if not key:
            return None
        ret = None
        i = bisect_left(self.key, key)
        if i:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect_left(block, key)
        if i:
            ret = block[i - 1]
        return ret

    def search_higher(self, key):
        if not key:
            return None
        ret = None
        i = bisect(self.key, key)
        if i < len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect(block, key)
        if i < len(block):
            ret = block[i]
        return ret

    def insert(self, key):
        i = bisect(self.key, key)
        block = self.child[i]
        insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1 :])
            self.child[i] = block[:sep]


def main():
    n, *a = list(map(int, open(0).read().split()))
    l = [0] * n
    for i, v in enumerate(a, 1):
        l[v - 1] = i
    t = SqrtSet()
    t.insert(0)
    t.insert(n + 1)
    c = 0
    for i, v in enumerate(l, 1):
        c += (t.search_higher(v) - v) * (v - t.search_lower(v)) * i
        t.insert(v)
    print(c)


main()

class UnionFind:
    def __init__(self, size):
        self.table = [-1] * size

    def root(self, x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def unite(self, x, y):
        s1 = self.root(x)
        s2 = self.root(y)
        if s1 != s2:
            if self.table[s1] > self.table[s2]:
                s1, s2 = s2, s1
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        return

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.table[self.root(x)]


n, *a = list(map(int, open(0).read().split()))
u = UnionFind(n)
b = [0] * n
for i, t in enumerate(a):
    b[t - 1] = i
c = 0
for k in b[::-1]:
    l = r = 1
    if k > 0 and a[k - 1] > a[k]:
        l += u.size(k - 1)
        u.unite(k - 1, k)
    if k + 1 < n and a[k + 1] > a[k]:
        r += u.size(k + 1)
        u.unite(k + 1, k)
    c += l * r * a[k]
print(c)

-from bisect import * +class UnionFind: +    def __init__(self, size): +        self.table = [-1] * size + +    def root(self, x): +        while self.table[x] >= 0: +            x = self.table[x] +        return x + +    def unite(self, x, y): +        s1 = self.root(x) +        s2 = self.root(y) +        if s1 != s2: +            if self.table[s1] > self.table[s2]: +                s1, s2 = s2, s1 +            self.table[s1] += self.table[s2] +            self.table[s2] = s1 +        return + +    def same(self, x, y): +        return self.root(x) == self.root(y) + +    def size(self, x): +        return -self.table[self.root(x)] -class SqrtSet: -    def __init__(self, block_limit=201): -        self.key = [] -        self.child = [[]] -        self.block_limit = block_limit - -    def search_lower(self, key): -        if not key: -            return None -        ret = None -        i = bisect_left(self.key, key) -        if i: -            ret = self.key[i - 1] -        block = self.child[i] -        i = bisect_left(block, key) -        if i: -            ret = block[i - 1] -        return ret - -    def search_higher(self, key): -        if not key: -            return None -        ret = None -        i = bisect(self.key, key) -        if i < len(self.key): -            ret = self.key[i] -        block = self.child[i] -        i = bisect(block, key) -        if i < len(block): -            ret = block[i] -        return ret - -    def insert(self, key): -        i = bisect(self.key, key) -        block = self.child[i] -        insort(block, key) -        if len(block) == self.block_limit: -            sep = self.block_limit // 2 -            self.key.insert(i, block[sep]) -            self.child.insert(i + 1, block[sep + 1 :]) -            self.child[i] = block[:sep] - - -def main(): -    n, *a = list(map(int, open(0).read().split())) -    l = [0] * n -    for i, v in enumerate(a, 1): -        l[v - 1] = i -    t = SqrtSet() -    t.insert(0) -    t.insert(n + 1) -    c = 0 -    for i, v in enumerate(l, 1): -        c += (t.search_higher(v) - v) * (v - t.search_lower(v)) * i -        t.insert(v) -    print(c) - - -main() +n, *a = list(map(int, open(0).read().split())) +u = UnionFind(n) +b = [0] * n +for i, t in enumerate(a): +    b[t - 1] = i +c = 0 +for k in b[::-1]: +    l = r = 1 +    if k > 0 and a[k - 1] > a[k]: +        l += u.size(k - 1) +        u.unite(k - 1, k) +    if k + 1 < n and a[k + 1] > a[k]: +        r += u.size(k + 1) +        u.unite(k + 1, k) +    c += l * r * a[k] +print(c)