N = int(raw_input())
n = map(int, raw_input().split())

n_min = min(n)

total = 0
for ind, num in enumerate(n):
    if num == n_min:
        total += num * (ind+1) * (N - ind)
        continue

    l, k = ind, ind

    while l >= 0:
        if num <= n[l]:
            l -= 1
        else:
            break

    while k < N:
        if num <= n[k]:
            k += 1
        else:
            break

    total += num * (ind-l) * (k-ind)

print total