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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include<iostream>
#include<vector>
#include<algorithm>
#include<complex>
using namespace std;

const double PI = acos(-1);

const int MAX_FFT = 1 << 24;
complex<double> vvt[MAX_FFT];
complex<double> vvvt[MAX_FFT];

int calc_size(int a, int b) {
    long long n = 1;
    while (n < a + b) 
        n <<= 1;
    return n;
}

int reverse(int num, int lg_n) {
    int res = 0;
    for (int i = 0; i < lg_n; i++) {
        if (num & (1 << i))
            res |= 1 << (lg_n - 1 - i);
    }
    return res;
}

void fft(complex<double> a[], int n, bool invert) {
    for (int i = 1, j = 0; i < n; i++) {
        int bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(a[i], a[j]);
    }

    for (int len = 2; len <= n; len <<= 1) {
        double ang = 2 * PI / len * (invert ? -1 : 1);
        complex<double> wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            complex<double> w(1);
            for (int j = 0; j < len / 2; j++) {
                complex<double> u = a[i+j], v = a[i+j+len/2] * w;
                a[i+j] = u + v;
                a[i+j+len/2] = u - v;
                w *= wlen;
            }
        }
    }

    if (invert) {
        for (int i = 0; i < n; i++) {
            a[i] /= n;
        }
    }
}

void multiply(vector<short> &freq, int sizea, int sizeb) {
    long long n = calc_size(sizea, sizeb);
    for (int i = 0; i < sizea; i++) {
        vvvt[i] = vvt[i];
    }

    for (int i = 0; i < n; i++) vvt[i] = 0;
    for (int i = 0; i < freq.size(); i++) {
        vvt[i] = freq[i];
    }

    fft(vvvt, n, false);
    fft(vvt, n, false);
    for (int i = 0; i < n; i++) {
        vvvt[i] *= vvt[i];
    }
    fft(vvvt, n, true);
}

int multiply_ffq(int sizea) {
    long long n = calc_size(sizea, sizea);
    fft(vvt, n, false);
    for (int i = 0; i < n; i++) {
        vvt[i] *= vvt[i];
    }
    fft(vvt, n, true);

    return n;
}

const int MAX = 500 * 500 + 5;
long long bicoff[MAX][4];

vector<short> count(vector<long long> &v, int N) {
    int n = v.size();
    vector<short> res(2 * N + 1);
    for (int i = 0; i < n; i++) {
        res[v[i]]++;
    }
    return res;
}

long long get_freq(vector<short> &freq, int k) {
    return k >= freq.size() || k < 0 ? 0 : freq[k];
}


int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<long long> tmp(n);
    for (int i = 0; i < n; i++) {
        cin >> tmp[i];
    }
    
    if (n == 1) {
        cout << 0 << endl;
        return 0;
    }

    // create BUC
    long long minn = 0;
    long long maxx = 0;
    vector<long long> v(n * (n + 1) / 2);
    for (int i = 0, k = 0; i < n; i++) {
        long long acc = 0;
        for (int j = i; j < n; j++) {
            acc += tmp[j];
            v[k++] = acc;
            minn = min(minn, acc);
            maxx = max(maxx, acc);
        }
    }
    sort(v.begin(), v.end());
    long long N = max(abs(minn), maxx);

    if (N > 4'000'000) {
        long long ret = 0;
        for (int i = 0; i < v.size() - 2; i++) {
            long long a = i + 1, b = v.size() - 1;
            while (a < b) {
                int acc = v[i] + v[a] + v[b];
                if (acc == 0)  {
                    if (v[a] == v[b]) {
                        ret += (b - a) * (b - a + 1) / 2;
                        break;
                    }
                    else {
                        long long ac = 1;
                        while (v[a + 1] == v[a]) {
                            ac++;
                            a++;
                        }
                        a++;
                        long long bc = 1;
                        while (v[b - 1] == v[b]) {
                            bc++;
                            b--;
                        }
                        b--;
                        ret += ac * bc;
                    }
                }
                else if (acc < 0) a++;
                else b--;
            }
        }
        cout << ret << endl;

        return 0;
    }

    // compute binomial coefficients
    bicoff[0][0] = 1;
    bicoff[0][1] = 0;
    bicoff[0][2] = 0;
    bicoff[0][3] = 0;
    for (int i = 1; i < MAX; i++) {
        bicoff[i][0] = 1;
        bicoff[i][1] = bicoff[i - 1][1] + bicoff[i - 1][0];
        bicoff[i][2] = bicoff[i - 1][2] + bicoff[i - 1][1];
        bicoff[i][3] = bicoff[i - 1][3] + bicoff[i - 1][2];
    }
    
    // shift BUC
    for (int i = 0; i < v.size(); i++) {
        v[i] += N;
    }

    // compute fft
    vector<short> freq = count(v, N);
    for (int i = 0; i < freq.size(); i++) {
        vvt[i] = freq[i];
    }

    int sizevvt = multiply_ffq(freq.size());
    multiply(freq, sizevvt, freq.size());

    // remove duples and triples
    long long wyn = round(vvvt[3 * N].real());
    for (int l = 0; l < N; l++) {
        long long sub = 3ll * get_freq(freq, l) * get_freq(freq, l) * get_freq(freq, 3 * N - 2 * l);
        wyn -= sub;
        int k = 3 * N - l;
        if (k % 2 == 0) {
            sub = 3ll * get_freq(freq, l) * get_freq(freq, k / 2) * get_freq(freq, k / 2);
            wyn -= sub;
        }
    }
    wyn -= get_freq(freq, N) * get_freq(freq, N) * get_freq(freq, N);
    wyn /= 6;

    // add dupples and triples
    for (int l = 0; l < N; l++) {
        long long add = get_freq(freq, l) * (get_freq(freq, l) - 1) / 2 * get_freq(freq, 3 * N - 2 * l);
        wyn += add;
        int k = 3 * N - l;
        if (k % 2 == 0) {
            add = get_freq(freq, l) * (get_freq(freq, k / 2) * (get_freq(freq, k / 2) - 1) / 2);
            wyn += add;
        }
    }
    if (get_freq(freq, N) >= 3) {
        wyn += bicoff[get_freq(freq, N)][3];
    }

    cout << wyn << endl;

    return 0;
}