n=int(eval(input()))

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

b=[0]*n

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

ans=0

l,r=[i for i in range(n+1)],[i for i in range(n+1)]

for i in range(n-1,-1,-1):

  ans+=(i+1)*(b[i]-l[b[i]]+1)*(r[b[i]]-b[i]+1)

  l[r[b[i]]+1],r[l[b[i]]-1]=l[b[i]],r[b[i]]

print(ans)
n=int(eval(input()))

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

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

r=[i+1 for i in range(n+1)]+[n+2]

ans=0

p=[0]*n

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

for i in range(n-1,-1,-1):

  j=p[i]

  ans+=(i+1)*(j-l[j])*(r[j]-j)

  l[r[j]],r[l[j]]=l[j],r[j]

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
b = [0] * n
for i in range(n):
    b[a[i] - 1] = i
ans = 0
l, r = [i for i in range(n + 1)], [i for i in range(n + 1)]
for i in range(n - 1, -1, -1):
    ans += (i + 1) * (b[i] - l[b[i]] + 1) * (r[b[i]] - b[i] + 1)
    l[r[b[i]] + 1], r[l[b[i]] - 1] = l[b[i]], r[b[i]]
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
l = [i - 1 for i in range(n + 1)] + [n]
r = [i + 1 for i in range(n + 1)] + [n + 2]
ans = 0
p = [0] * n
for i in range(n):
    p[a[i] - 1] = i
for i in range(n - 1, -1, -1):
    j = p[i]
    ans += (i + 1) * (j - l[j]) * (r[j] - j)
    l[r[j]], r[l[j]] = l[j], r[j]
print(ans)

-b = [0] * n +l = [i - 1 for i in range(n + 1)] + [n] +r = [i + 1 for i in range(n + 1)] + [n + 2] +ans = 0 +p = [0] * n -    b[a[i] - 1] = i -ans = 0 -l, r = [i for i in range(n + 1)], [i for i in range(n + 1)] +    p[a[i] - 1] = i -    ans += (i + 1) * (b[i] - l[b[i]] + 1) * (r[b[i]] - b[i] + 1) -    l[r[b[i]] + 1], r[l[b[i]] - 1] = l[b[i]], r[b[i]] +    j = p[i] +    ans += (i + 1) * (j - l[j]) * (r[j] - j) +    l[r[j]], r[l[j]] = l[j], r[j]