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