N = int(input()) A = list(map(int, input().split())) ind = [0] * N for i in range(N): ind[A[i]] = i left = right = ind[0] ans = 0 for i in range(1, N): now = ind[i] if left < now < right: pass elif now > right: ans += (now - right - 1) * i ans += left * i ans += i ans += (now - right - 1) * left * i right = now else: ans += (N - 1 - right) * i ans += (left - now - 1) * i ans += i ans += (N - 1 - right) * (left - now - 1) * i left = now print(ans + N)