n = int(eval(input()))

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

p = [0]*(n+1)

ans = 0

for i in range(n):

  p[a[i]] = i+1

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

l[0] = 0

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

r[-1] = n+1

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

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

  l[r[p[i]]] = l[p[i]]

  r[l[p[i]]] = r[p[i]]

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

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



# indexをソート

b = [0] * N

for i in range(N):

    b[a[i] - 1] = i



# 初期化　端っこ対策としてひとつ余計に取っている

left = [0] * (N + 1)

right = [0] * (N + 1)

for i in range(N + 1):

    left[i] = i-1

    right[i] = i+1



# 更新

for idx in reversed(b):

    left[right[idx]] = left[idx]

    right[left[idx]] = right[idx]



ans = 0

for i in range(N):

    ans += a[i] * (i - left[i]) * (right[i] - i)



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

N = int(eval(input()))
a = list(map(int, input().split()))
# indexをソート
b = [0] * N
for i in range(N):
    b[a[i] - 1] = i
# 初期化　端っこ対策としてひとつ余計に取っている
left = [0] * (N + 1)
right = [0] * (N + 1)
for i in range(N + 1):
    left[i] = i - 1
    right[i] = i + 1
# 更新
for idx in reversed(b):
    left[right[idx]] = left[idx]
    right[left[idx]] = right[idx]
ans = 0
for i in range(N):
    ans += a[i] * (i - left[i]) * (right[i] - i)
print(ans)

-n = int(eval(input())) +N = int(eval(input())) -p = [0] * (n + 1) +# indexをソート +b = [0] * N +for i in range(N): +    b[a[i] - 1] = i +# 初期化　端っこ対策としてひとつ余計に取っている +left = [0] * (N + 1) +right = [0] * (N + 1) +for i in range(N + 1): +    left[i] = i - 1 +    right[i] = i + 1 +# 更新 +for idx in reversed(b): +    left[right[idx]] = left[idx] +    right[left[idx]] = right[idx] -for i in range(n): -    p[a[i]] = i + 1 -l = [i - 1 for i in range(n + 2)] -l[0] = 0 -r = [i + 1 for i in range(n + 2)] -r[-1] = n + 1 -for i in range(n, 0, -1): -    ans += (p[i] - l[p[i]]) * (r[p[i]] - p[i]) * i -    l[r[p[i]]] = l[p[i]] -    r[l[p[i]]] = r[p[i]] +for i in range(N): +    ans += a[i] * (i - left[i]) * (right[i] - i)