from collections import defaultdict n = int(input()) a = list(map(int, input().split())) res = 0 d = defaultdict(int) for i in range(n): c = d[a[i] - 1] res += c d[a[i]] += c + 1 print(res)