N = int(input()) A = list(map(int,input().split())) d = [0] * N for i in range(N): d[A[i]] = i left = d[0] right = d[0] ans = 0 for i in range(1,N): pos = d[i] if left <= pos <= right: continue if pos > right: l = left + 1 r = pos - right ans += i *l * r right = pos elif pos < left: l = left - pos r = N - right ans += i * l * r left = pos print(ans+N)