1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import collections
n = int(input())
tab = [int(x) for x in input().split()]
subseq = collections.defaultdict(int)

for i in range(n):
    current_sum = 0
    for j in range(i, n):
        current_sum += tab[j]
        subseq[current_sum] += 1

#print(subseq)
result = 0
for i in subseq:
    if i == 0:
        result += (subseq[i] * (subseq[i] -1) * (subseq[i] - 2))

    if -2 * i in subseq and i:
        pairs = (subseq[i] * subseq[i] -1) // 2
        result += pairs * subseq[-2 * i] * 6
    for j in subseq:
        if i == j or 2 * i == -j or 2 * j == -i:
            continue
        if -i -j in subseq:
            result += subseq[i] * subseq[j] * subseq[-i - j]

print(result // 6)