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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "bits/stdc++.h"

using namespace std;


int main() {
    int n; cin >> n;
	vector<int> A(n);
	for(auto &a : A) cin >> a;

	int max_B = INT_MIN;
	int min_B = INT_MAX;
	vector<int> B;
	for(int i=0; i<n; i++){
		int sum = 0;
		for(int j=i; j<n; j++){
			sum += A[j];
			max_B=max(max_B, sum);
			min_B=min(min_B, sum);
			B.push_back(sum);
		}
	}


	/*
	vector<int> ile_razy(max_B - min_B + 1);
	for(auto b : B) ile_razy[b - min_B]++;
	
	auto C = conv_mod(ile_razy, ile_razy, 1e9+7);
	C = conv_mod(C, ile_razy, 1e9+7);

	int res = C[-3*min_B];
	
	for(int i=0; i<ile_razy.size(); i++) {
		if((-3 * min_B - i) % 2 == 0 && (-3 * min_B - i)/2 < C.size())
			res -= 2 * ile_razy[i] * ile_razy[(-3 * min_B - i)/2];
	}

	cout << res << '\n';
	*/

	unordered_map<int, int> ile_razy;
	for(auto b : B) ile_razy[b]++;

	int res=0;
    sort(B.begin(), B.end());
	for(int i=0; i<B.size(); i++){
        int a = B[i];
        int start = i+1;
        int end = B.size()-1;
        while(start < end) {
            int b = B[start];
            int c = B[end];
            if(a+b+c == 0) {
                if(b == c){
                    res += (end-start) * (end-start+1) / 2;
                    break;
                }

                int old_start = start;
                while(B[start] == B[old_start]) start++;

                int old_end = end;
                while(B[end] == B[old_end]) end--;

                res += (start-old_start) * (old_end-end);

                continue;
            }

            if(a+b+c > 0){
                end--;
            } else start++;
        }
    }


	cout << res << '\n';
}