from collections import defaultdict N=int(input()) A=list(map(int,input().split())) d=defaultdict(int) cnt=defaultdict(int) for i in range(N): cnt[A[i]]+=1 d[A[i]]+=d[A[i]-1]+cnt[A[i]-1] print(sum(d.values()))