N = int(input()) A = list(map(int, input().split())) p = [None] * N for i in range(N): p[A[i]] = i ans = 0 left, right = N, -1 for i in range(N): left, right = min(left, p[i]), max(right, p[i]) ans += (left + 1) * (N - right) print(ans)