from collections import * N = int(input()) A = list(map(int, input().split())) D = defaultdict(int) ans = 0 for a in A: ans += D[a - 1] D[a] += D[a - 1] + 1 print(ans)