import sys input = sys.stdin.readline N=int(input()) A=list(map(int,input().split())) A_INV=[-1]*N for i in range(N): A_INV[A[i]]=i left=1<<60 right=-1 ANS=0 for i in range(N): left=min(left,A_INV[i]) right=max(right,A_INV[i]) ANS+=(left+1)*(N-right) print(ANS)