N = int(input()) A = tuple(map(int, input().split())) indices = [0] * N for i, a in enumerate(A): indices[a] = i ans = 0 l = N r = 0 for x in indices: l = min(l, x) r = max(r, x) ans += (l + 1) * (N - r) print(ans)