from collections import * def calc(A): D = defaultdict(int) val = 0 for i in range(N): v = D[A[i] - 1] D[A[i]] += v + 1 val += v return val N = int(input()) A = list(map(int, input().split())) print(calc(A))