#import decimal

#decimal.getcontext().prec==500 Floatの桁数を500まで増やす

#Decimal(c).sqrt() それで平方根を求める

#Decimal(1) / Decimal(7) 1/7

from collections import *

from itertools import *

from bisect import *

from heapq import *

import copy

import math

from fractions import gcd

import sys

#input = sys.stdin.readline





#N=int(input())



N=int(eval(input()))

A=list(map(int,input().split()))



lst=[[A[0],0,1]]

count=0



for i in range(1,N):

    m=1

    while lst and lst[-1][0]>A[i]:

        a,b,c=lst.pop()

        count+=a*(i-b)*c

        m+=c

    lst.append([A[i],i,m])

while lst:

    a,b,c=lst.pop()

    count+=a*(N-b)*c



print(count)

#import decimal

#decimal.getcontext().prec==500 Floatの桁数を500まで増やす

#Decimal(c).sqrt() それで平方根を求める

#Decimal(1) / Decimal(7) 1/7

"""

from collections import *

from itertools import *

from bisect import *

from heapq import *

import copy

import math

from fractions import gcd

import sys"""

#input = sys.stdin.readline





#N=int(input())



N=int(eval(input()))

A=list(map(int,input().split()))



lst=[[A[0],0,1]]

count=0



for i in range(1,N):

    m=1

    while lst and lst[-1][0]>A[i]:

        a,b,c=lst.pop()

        count+=a*(i-b)*c

        m+=c

    lst.append([A[i],i,m])

while lst:

    a,b,c=lst.pop()

    count+=a*(N-b)*c



print(count)

# import decimal
# decimal.getcontext().prec==500 Floatの桁数を500まで増やす
# Decimal(c).sqrt() それで平方根を求める
# Decimal(1) / Decimal(7) 1/7
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys

# input = sys.stdin.readline
# N=int(input())
N = int(eval(input()))
A = list(map(int, input().split()))
lst = [[A[0], 0, 1]]
count = 0
for i in range(1, N):
    m = 1
    while lst and lst[-1][0] > A[i]:
        a, b, c = lst.pop()
        count += a * (i - b) * c
        m += c
    lst.append([A[i], i, m])
while lst:
    a, b, c = lst.pop()
    count += a * (N - b) * c
print(count)

# import decimal
# decimal.getcontext().prec==500 Floatの桁数を500まで増やす
# Decimal(c).sqrt() それで平方根を求める
# Decimal(1) / Decimal(7) 1/7
"""
from collections import *
from itertools import *
from bisect import *
from heapq import *
import copy
import math
from fractions import gcd
import sys"""
# input = sys.stdin.readline
# N=int(input())
N = int(eval(input()))
A = list(map(int, input().split()))
lst = [[A[0], 0, 1]]
count = 0
for i in range(1, N):
    m = 1
    while lst and lst[-1][0] > A[i]:
        a, b, c = lst.pop()
        count += a * (i - b) * c
        m += c
    lst.append([A[i], i, m])
while lst:
    a, b, c = lst.pop()
    count += a * (N - b) * c
print(count)

+""" -import sys - +import sys"""