from collections import defaultdict from itertools import accumulate N = int(input()) A = list(map(int, input().split())) def helper(A): N = len(A) W = list(accumulate(A, initial=0)) dp = [0]*(30+1) for x in range(1, 31): d = defaultdict(int) term = 0 for i in range(N): t = W[i+1] - (i+1) * x if A[i] == x: term += d[t] s = W[i] - i*x d[s] += 1 dp[x] += term return sum(dp) def resolve(A): N = len(A) W = list(accumulate(A, initial=0)) dp = [0]*(30+1) for x in range(1, 31): d = defaultdict(int) term = 0 for i in range(N): if A[i] != x: continue t = W[i+1] - (i+1) * x term += d[t] s = W[i] - i*x d[s] += 1 dp[x] += term return sum(dp) ans = N*(N-1) // 2 ans -= helper(A) ans -= helper(A[::-1]) ans += resolve(A) print(ans+1)