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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <bits/stdc++.h>

using namespace std;

const int N = 500 + 7;
const int A = 20000 + 7;

// https://judge.yosupo.jp/submission/195925
const int MOD = 998244353;
const long long MOD2 = (long long) MOD * MOD;
const int root = 3;
const int alim = 64; // Bound for using O(n^2) polynomial mult 

int modpow(int b, int e) {
	int ans = 1;
	for (; e; b = (long long) b * b % MOD, e /= 2)
		if (e & 1) ans = (long long) ans * b % MOD;
	return ans;
}

const int MODinv = 2 - MOD; // pow(-MOD, -1, 2**32)
inline int m_reduce(long long x) {
    int m = x * MODinv;
    return (x>>32) - (((long long) m * MOD) >> 32);
}

const int r2 = modpow(2, 64);
inline int m_transform(int x) {
    return m_reduce((long long)x * r2);
}

inline int m_add(int x, int y) {
    int z = x + y;
    return z < 0 ? z + MOD : z - MOD;
}

inline int m_sub(int x, int y) {
    int z = x - y;
    return z < 0 ? z + MOD : z - MOD;
}

inline int m_mult(int x, int y) {
    return m_reduce((long long) x * y);
}

vector<int> rt = {1};
vector<int> transformed_rt;
vector<int> transformed_rt2;

template<int a>
void transform(vector<int> &P) {
    int m = P.size();
    int n = m / a;

    int size = rt.size();
    while (2 * size < n) {
        rt.resize(n / 2);
        int r = modpow(root, MOD / (4 * size));
        for (int i = 0; i < size; ++i)
            rt[i + size] = (long long) r * rt[i] % MOD;
        size *= 2;
    }

    // For montgomery
    for (int i = transformed_rt.size(); i < rt.size(); ++i) {
        transformed_rt.resize(rt.size());
        transformed_rt[i] = m_transform(rt[i]);
        transformed_rt2.resize(rt.size());
        transformed_rt2[i] = (unsigned int) MODinv * transformed_rt[i];
    }

    // Radix 4 recursive NTT
    auto dfs = [&](auto &&self, int i, int k) -> void {
        if (k == 1)
          return;
        int step = k * a;
        int quarter_step = step / 4;

        int R20 = transformed_rt2[2 * i];
        int RR0 = transformed_rt[2 * i];
        
        int R21 = transformed_rt2[2 * i + 1];
        int RR1 = transformed_rt[2 * i + 1];

        int R2 = transformed_rt2[i];
        int RR = transformed_rt[i];
        
        int *P1 = &P[i * step];
        int *P2 = P1 + quarter_step;
        int *P3 = P2 + quarter_step;
        int *P4 = P3 + quarter_step;

        #pragma GCC ivdep
        for (int j = 0; j < quarter_step; ++j) {
            int z0;
            {
                int z = P3[j];
                int m = (unsigned int) R2 * z;
                z0 = ((long long) z * RR - (long long) m * MOD) >> 32;
            }

            int z1;
            {
                int z = P4[j];
                int m = (unsigned int) R2 * z;
                z1 = ((long long) z * RR - (long long) m * MOD) >> 32;
            }

            int sum0 = m_add(P1[j], z0);
            int diff0 = m_sub(P1[j], z0);
            int sum1 = P2[j] + z1;
            int diff1 = P2[j] - z1;

            // [sum0, sum1, diff0, diff1]
            
            int zz0;
            {
                int z = sum1;
                int m = (unsigned int) R20 * z;
                zz0 = ((long long) z * RR0 - (long long) m * MOD) >> 32;
            }

            int zz1;
            {
                int z = diff1;
                int m = (unsigned int) R21 * z;
                zz1 = ((long long) z * RR1 - (long long) m * MOD) >> 32;
            }

            P1[j]  = m_add(sum0, zz0);
            P2[j]  = m_sub(sum0, zz0);
            P3[j]  = m_add(diff0, zz1);
            P4[j]  = m_sub(diff0, zz1);
        }

        self(self, 4*i+0, k/4);
        self(self, 4*i+1, k/4);
        self(self, 4*i+2, k/4);
        self(self, 4*i+3, k/4);
    };

    int k = n;
    while (k >= 4) k /= 4;

    if (k == 2) {
        int step = n * a;
        int half_step = step / 2;
        for (int j1 = 0; j1 < half_step; ++j1) {
            int j2 = j1 + half_step;

            int diff = m_sub(P[j1], P[j2]);
            P[j1] = m_add(P[j1], P[j2]);
            P[j2] = diff;
        }
        k = n/2;
        dfs(dfs, 0, k);
        dfs(dfs, 1, k);
    } else {
        k = n;
        dfs(dfs, 0, k);
    }

    for (int i = 0; i < m; ++i)
        if (P[i] < 0) P[i] += MOD;
}

template<int a>
void inverse_transform(vector<int> &P) {
    int m = P.size();
    int n = m / a;
    int n_inv = m_transform(modpow(n, MOD - 2));

    vector<int> rev(n);
    for (int i = 1; i < n; ++i) {
        rev[i] = rev[i / 2] / 2 + (i & 1) * n / 2;
    }

    // P = [p * n_inv for p in P]
    for (int i = 0; i < m; ++i)
        P[i] = m_mult(n_inv, P[i]);
    
    // P = [P[a * rev[i // a] + (i % a)] for i in range(m)]
    for (int i = 1; i < n; ++i)
        if (i < rev[i])
            swap_ranges(P.begin() + a * i, P.begin() + a * i + a, P.begin() + a * rev[i]);
    
    // P = [P[-a * (i // a) + (i % a)] for i in range(m)]
    for (int i = 1; i < n/2; ++i)
        swap_ranges(P.begin() + a * i, P.begin() + a * i + a, P.begin() + a * (n - i));

    transform<a>(P);

    // P = [P[a * rev[i // a] + (i % a)] for i in range(m)]
    for (int i = 1; i < n; ++i)
        if (i < rev[i])
            swap_ranges(P.begin() + a * i, P.begin() + a * i + a, P.begin() + a * rev[i]);
}

template<int a>
void fast_polymult_mod(vector<int> &P, vector<int> &Q) {
    int m = P.size();
    int n = m / a;

    transform<a>(P);
    transform<a>(Q);

    vector<int> &PQ = P;
    for (int i = 0; i < n; ++i) {
        vector<unsigned long long> res(2 * a);
        for (int j = 0; j < a; ++j) {
            if (j >= 10 && j % 9 == 8)
                for (int k = j; k < j + a - 10; ++k)
                    res[k] -= (res[k] >> 63) * 9 * MOD2;
            for (int k = 0; k < a; ++k)
                res[j + k] += (long long) P[i * a + j] * Q[i * a + k];
        }

        int c = rt[i/2];
        if (i & 1) c = MOD - c;
        for (int j = 0; j < a; ++j)
            PQ[i * a + j] = (res[j] + c * (res[j + a] % MOD)) % MOD;
    }

    inverse_transform<a>(PQ);
}

template <size_t... N>
void work(std::index_sequence<N...>, int x, std::vector<int>& a, std::vector<int>& b) {
    static void (*ptrs[])(std::vector<int>&, std::vector<int>&) = {&fast_polymult_mod<N+1>...};
    ptrs[x - 1](a, b);
}

void fast_polymult(vector<int> &P, vector<int> &Q) {
    int m1 = P.size();
    int m2 = Q.size();
    int res_len = m1 + m2 - 1;

    int b = 1;
    while ((alim << b) < res_len) ++b;
    int a = ((res_len - 1) >> b) + 1;
    int m = a << b;

    P.resize(m);
    Q.resize(m);
    
    // Call fast_polymult_mod<a>(P, Q);
    work(std::make_index_sequence<alim>{}, a, P, Q);

    P.resize(res_len);
}

int n, a[N];

vector<int> square(vector<int> P) {
	vector<int> Q = P;
	fast_polymult(P, Q);
	return P;

	/*
	vector<int> Q(2 * P.size() - 1);

	for (int i = 0; i < (int) P.size(); i++)
		for (int j = 0; j < (int) P.size(); j++)
			Q[i + j] += P[i] * P[j];

	return Q;
	*/
}


int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);

	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}

	vector<int> b;
	int zero = 0;

	for (int i = 1; i <= n; i++) {
		int sum = 0;
		for (int j = i; j <= n; j++) {
			sum += a[j];

			if (sum == 0) {
				zero++;
			}
			else {
				b.push_back(sum);
			}
		}
	}

	int shift = (b.empty() ? 0 : *min_element(b.begin(), b.end()));
	int mx = (b.empty() ? 0 : *max_element(b.begin(), b.end()));

	long long ans = 1LL * zero * (zero - 1) * (zero - 2) / 6;

	if (shift > 0 || mx < 0) {
		cout << ans << '\n';
		return 0;
	}

	vector<int> Q(mx - shift + 1);

	for (int x : b) {
		Q[x - shift]++;
	}

	cerr << "sz: " << Q.size() << '\n';
	vector<int> P = square(Q);

	for (int x : b) {
		P[2 * (x - shift)]--;
	}

	for (int i = 0; i < (int) P.size(); i++)
		P[i] /= 2;
	
	ans += 1LL * P[2 * (0 - shift)] * zero;

	long long three = 0;
	for (int x : b)
		if (0 <= -x - 2 * shift && -x - 2 * shift < (int) P.size()) {
			three += P[-x - 2 * shift];

			if (0 <= -2 * x - shift && -2 * x - shift < (int) Q.size()) {
				three -= Q[-2 * x - shift];
			}
		}

	ans += three / 3;

	cout << ans << '\n';

}