import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) idx = [-1]*N for i in range(N): idx[A[i]] = i l, r = idx[0], idx[0] ans = 0 for i in range(1, N): if lr: ans += i*(l+1)*(idx[i]-r) r = idx[i] else: ans += i*(l-idx[i])*(N-r) l = idx[i] print(ans+N)