N = int(input()) A = list(map(int,input().split())) ans = 0 left = 0 while left < N: right = left + 1 if right < N and A[left] + 1 == A[right]: right += 1 while right < N and A[right-1] + 1 == A[right]: right += 1 tmp = right - left ans += tmp * (tmp - 1) // 2 left = right elif right < N and A[left] - 1 == A[right]: right += 1 while right < N and A[right - 1] - 1 == A[right]: right += 1 tmp = right - left ans += tmp * (tmp - 1) // 2 left = right else: left += 1 print(ans)