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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <vector>
#include <stdint.h>
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define re(i, n) FOR(i, 1, n)
#define ford(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pll> vpll;
typedef vector<pii> vpii;

const int NN = 3e7;

// zajebane z https://codeforces.com/contest/1251/submission/101133210


namespace IO
{
    const int BUFFER_SIZE = 1 << 15;

    char input_buffer[BUFFER_SIZE];
    int input_pos = 0, input_len = 0;

    char output_buffer[BUFFER_SIZE];
    int output_pos = 0;

    char number_buffer[100];
    uint8_t lookup[100];

    void _update_input_buffer()
    {
        input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin);
        input_pos = 0;

        if (input_len == 0)
            input_buffer[0] = EOF;
    }

    inline char next_char(bool advance = true)
    {
        if (input_pos >= input_len)
            _update_input_buffer();

        return input_buffer[advance ? input_pos++ : input_pos];
    }

    template <typename T>
    inline void read_int(T &number)
    {
        bool negative = false;
        number = 0;

        while (!isdigit(next_char(false)))
            if (next_char() == '-')
                negative = true;

        do
        {
            number = 10 * number + (next_char() - '0');
        } while (isdigit(next_char(false)));

        if (negative)
            number = -number;
    }

    template <typename T, typename... Args>
    inline void read_int(T &number, Args &...args)
    {
        read_int(number);
        read_int(args...);
    }

    void _flush_output()
    {
        fwrite(output_buffer, sizeof(char), output_pos, stdout);
        output_pos = 0;
    }

    inline void write_char(char c)
    {
        if (output_pos == BUFFER_SIZE)
            _flush_output();

        output_buffer[output_pos++] = c;
    }

    template <typename T>
    inline void write_int(T number, char after = '\0')
    {
        if (number < 0)
        {
            write_char('-');
            number = -number;
        }

        int length = 0;

        while (number >= 10)
        {
            uint8_t lookup_value = lookup[number % 100];
            number /= 100;
            number_buffer[length++] = (lookup_value & 15) + '0';
            number_buffer[length++] = (lookup_value >> 4) + '0';
        }

        if (number != 0 || length == 0)
            write_char(number + '0');

        for (int i = length - 1; i >= 0; i--)
            write_char(number_buffer[i]);

        if (after)
            write_char(after);
    }

    void init()
    {
        // Make sure _flush_output() is called at the end of the program.
        bool exit_success = atexit(_flush_output) == 0;
        assert(exit_success);

        for (int i = 0; i < 100; i++)
            lookup[i] = (i / 10 << 4) + i % 10;
    }
}

const int MOD = 998244353;

struct mod_int
{
    int val;

    mod_int(long long v = 0)
    {
        if (v < 0)
            v = v % MOD + MOD;

        if (v >= MOD)
            v %= MOD;

        val = v;
    }

    static int mod_inv(int a, int m = MOD)
    {
        // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
        int g = m, r = a, x = 0, y = 1;

        while (r != 0)
        {
            int q = g / r;
            g %= r;
            swap(g, r);
            x -= q * y;
            swap(x, y);
        }

        return x < 0 ? x + m : x;
    }

    explicit operator int() const
    {
        return val;
    }

    mod_int &operator+=(const mod_int &other)
    {
        val += other.val;
        if (val >= MOD)
            val -= MOD;
        return *this;
    }

    mod_int &operator-=(const mod_int &other)
    {
        val -= other.val;
        if (val < 0)
            val += MOD;
        return *this;
    }

    static unsigned fast_mod(uint64_t x, unsigned m = MOD)
    {
#if !defined(_WIN32) || defined(_WIN64)
        return x % m;
#endif
        // Optimized mod for Codeforces 32-bit machines.
        // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit integer.
        unsigned x_high = x >> 32, x_low = (unsigned)x;
        unsigned quot, rem;
        asm("divl %4\n"
            : "=a"(quot), "=d"(rem)
            : "d"(x_high), "a"(x_low), "r"(m));
        return rem;
    }

    mod_int &operator*=(const mod_int &other)
    {
        val = fast_mod((uint64_t)val * other.val);
        return *this;
    }

    mod_int &operator/=(const mod_int &other)
    {
        return *this *= other.inv();
    }

    friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; }
    friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; }
    friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; }
    friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; }

    mod_int &operator++()
    {
        val = val == MOD - 1 ? 0 : val + 1;
        return *this;
    }

    mod_int &operator--()
    {
        val = val == 0 ? MOD - 1 : val - 1;
        return *this;
    }

    mod_int operator++(signed)
    {
        mod_int before = *this;
        ++*this;
        return before;
    }
    mod_int operator--(signed)
    {
        mod_int before = *this;
        --*this;
        return before;
    }

    mod_int operator-() const
    {
        return val == 0 ? 0 : MOD - val;
    }

    bool operator==(const mod_int &other) const { return val == other.val; }
    bool operator!=(const mod_int &other) const { return val != other.val; }

    mod_int inv() const
    {
        return mod_inv(val);
    }

    mod_int pow(long long p) const
    {
        assert(p >= 0);
        mod_int a = *this, result = 1;

        while (p > 0)
        {
            if (p & 1)
                result *= a;

            a *= a;
            p >>= 1;
        }

        return result;
    }

    friend ostream &operator<<(ostream &stream, const mod_int &m)
    {
        return stream << m.val;
    }
};

vector<mod_int> inv, factorial, inv_factorial;

void prepare_factorials(int maximum)
{
    inv.assign(maximum + 1, 1);

    // Make sure MOD is prime, which is necessary for the inverse algorithm below.
    for (int p = 2; p * p <= MOD; p++)
        assert(MOD % p != 0);

    for (int i = 2; i <= maximum; i++)
        inv[i] = inv[MOD % i] * (MOD - MOD / i);

    factorial.resize(maximum + 1);
    inv_factorial.resize(maximum + 1);
    factorial[0] = inv_factorial[0] = 1;

    for (int i = 1; i <= maximum; i++)
    {
        factorial[i] = i * factorial[i - 1];
        inv_factorial[i] = inv[i] * inv_factorial[i - 1];
    }
}

mod_int choose(long long n, long long r)
{
    if (r < 0 || r > n)
        return 0;

    return factorial[n] * inv_factorial[r] * inv_factorial[n - r];
}

mod_int inv_choose(long long n, long long r)
{
    assert(0 <= r && r <= n);
    return inv_factorial[n] * factorial[r] * factorial[n - r];
}

mod_int permute(long long n, long long k)
{
    if (k < 0 || k > n)
        return 0;

    return factorial[n] * inv_factorial[n - k];
}

mod_int inv_permute(long long n, long long k)
{
    assert(0 <= k && k <= n);
    return inv_factorial[n] * factorial[n - k];
}

namespace NTT
{
    vector<mod_int> roots = {0, 1};
    vector<int> bit_reverse;
    int max_size = -1;
    mod_int root;

    bool is_power_of_two(int n)
    {
        return (n & (n - 1)) == 0;
    }

    int round_up_power_two(int n)
    {
        assert(n > 0);

        while (n & (n - 1))
            n = (n | (n - 1)) + 1;

        return n;
    }

    // Given n (a power of two), finds k such that n == 1 << k.
    int get_length(int n)
    {
        assert(is_power_of_two(n));
        return __builtin_ctz(n);
    }

    // Rearranges the indices to be sorted by lowest bit first, then second lowest, etc., rather than highest bit first.
    // This makes even-odd div-conquer much easier.
    void bit_reorder(int n, vector<mod_int> &values)
    {
        if ((int)bit_reverse.size() != n)
        {
            bit_reverse.assign(n, 0);
            int length = get_length(n);

            for (int i = 0; i < n; i++)
                bit_reverse[i] = (bit_reverse[i >> 1] >> 1) + ((i & 1) << (length - 1));
        }

        for (int i = 0; i < n; i++)
            if (i < bit_reverse[i])
                swap(values[i], values[bit_reverse[i]]);
    }

    void find_root()
    {
        int order = MOD - 1;
        max_size = 1;

        while (order % 2 == 0)
        {
            order /= 2;
            max_size *= 2;
        }

        root = 2;

        // Find a max_size-th primitive root of MOD.
        while (!(root.pow(max_size) == 1 && root.pow(max_size / 2) != 1))
            root++;
    }

    void prepare_roots(int n)
    {
        if (max_size < 0)
            find_root();
        assert(n <= max_size);

        if ((int)roots.size() >= n)
            return;

        int length = get_length(roots.size());
        roots.resize(n);

        // The roots array is set up such that for a given power of two n >= 2, roots[n / 2] through roots[n - 1] are
        // the first half of the n-th primitive roots of MOD.
        while (1 << length < n)
        {
            // z is a 2^(length + 1)-th primitive root of MOD.
            mod_int z = root.pow(max_size >> (length + 1));

            for (int i = 1 << (length - 1); i < 1 << length; i++)
            {
                roots[2 * i] = roots[i];
                roots[2 * i + 1] = roots[i] * z;
            }

            length++;
        }
    }

    void fft_iterative(int N, vector<mod_int> &values)
    {
        assert(is_power_of_two(N));
        prepare_roots(N);
        bit_reorder(N, values);

        for (int n = 1; n < N; n *= 2)
            for (int start = 0; start < N; start += 2 * n)
                for (int i = 0; i < n; i++)
                {
                    mod_int even = values[start + i];
                    mod_int odd = values[start + n + i] * roots[n + i];
                    values[start + n + i] = even - odd;
                    values[start + i] = even + odd;
                }
    }

    const int FFT_CUTOFF = 150;

    vector<mod_int> mod_multiply(vector<mod_int> left, vector<mod_int> right)
    {
        int n = left.size();
        int m = right.size();

        // Brute force when either n or m is small enough.
        if (min(n, m) < FFT_CUTOFF)
        {
            const uint64_t ULL_BOUND = numeric_limits<uint64_t>::max() - (uint64_t)MOD * MOD;
            vector<uint64_t> result(n + m - 1);

            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                {
                    result[i + j] += (uint64_t)((int)left[i]) * ((int)right[j]);

                    if (result[i + j] > ULL_BOUND)
                        result[i + j] %= MOD;
                }

            for (uint64_t &x : result)
                if (x >= MOD)
                    x %= MOD;

            return vector<mod_int>(result.begin(), result.end());
        }

        int N = round_up_power_two(n + m - 1);
        left.resize(N);
        right.resize(N);

        bool equal = left == right;
        fft_iterative(N, left);

        if (equal)
            right = left;
        else
            fft_iterative(N, right);

        mod_int inv_N = mod_int(N).inv();

        for (int i = 0; i < N; i++)
            left[i] *= right[i] * inv_N;

        reverse(left.begin() + 1, left.end());
        fft_iterative(N, left);
        left.resize(n + m - 1);
        return left;
    }

    vector<mod_int> mod_power(const vector<mod_int> &v, int exponent)
    {
        assert(exponent >= 0);
        vector<mod_int> result = {1};

        if (exponent == 0)
            return result;

        for (int k = 31 - __builtin_clz(exponent); k >= 0; k--)
        {
            result = mod_multiply(result, result);

            if (exponent >> k & 1)
                result = mod_multiply(result, v);
        }

        return result;
    }
}

void add(vector<mod_int> &answers, const vector<mod_int> &product, size_t offset)
{
    answers.resize(max(answers.size(), product.size() + offset), 0);

    for (size_t i = 0; i < product.size(); i++)
        answers[offset + i] += product[i];
}

ll chujdupachuj[NN] = {0};

const ll BCONST = 2'000'000;
const ll NEGTCONST = 1e7 + 2;
//const ll BCONST = 100;

struct neg_table
{
    ll& operator[](int idx)
    {
        return chujdupachuj[idx + NEGTCONST];
    }
};

vll pos_big;
vll neg_big;
vll sumsv;
vpll sumsvp;

neg_table dupamap;

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> in(n);
    vector<mod_int> fft_dupa(2 * BCONST + 1);
    int zeros = 0;
    rep(i, n) cin >> in[i];
    rep(i, n)
    {
        ll sum = 0ll;
        for (int j = i; j < n; j++)
        {
            sum += in[j];
            if (sum == 0)
                zeros++;
            else
            {
                if (sum >= BCONST)
                {
                    pos_big.push_back(sum);
                }
                else if (sum <= -BCONST)
                {
                    neg_big.push_back(sum);
                }
                else
                {
                    fft_dupa[sum + BCONST]++;
                }
                dupamap[sum]++;
                sumsv.push_back(sum);
            }
        }
    }
    sort(all(sumsv));
    int cntttdupa = 1;
    for (int i = 1; i < sz(sumsv); i++)
    {
        if (sumsv[i - 1] != sumsv[i])
        {
            sumsvp.push_back({sumsv[i - 1], cntttdupa});
            cntttdupa = 1;
        }
        else
        {
            cntttdupa++;
        }
    }
    if (!sumsv.empty())
    {
        sumsvp.push_back({sumsv.back(), cntttdupa});
    }
    vector<mod_int> ffted_dupa = NTT::mod_multiply(fft_dupa, fft_dupa);
    ll ans = 0ll;
    ll ans2 = 0ll;
    for (int i = 0; i < sz(ffted_dupa); i++)
    {
        int sval = i;
        sval -= 2 * BCONST;
        ffted_dupa[i].val += MOD;
        ffted_dupa[i].val %= MOD;
        /*if (sval % 2 == 0)
        {
            int vvidx = (sval / 2) + BCONST;
            ffted_dupa[i].val -= (fft_dupa[vvidx].val * fft_dupa[vvidx].val);
            ffted_dupa[i].val += (fft_dupa[vvidx].val * (fft_dupa[vvidx].val-1)); //SUS
        }*/
        if (dupamap[-sval])
        {
            int chuj = ffted_dupa[i].val;
            if (-sval >= BCONST || -sval <= -BCONST)
            {
                ans2 += chuj * dupamap[-sval];
            }
            else
            {
                ans += chuj * dupamap[-sval];
            }
        }
    }
    ll ans3 = 0ll;
    for (auto& vp : sumsvp)
    {
        ll chuj = vp.first;
        ll chuj2 = -2 * chuj;
        if (chuj < BCONST && chuj > -BCONST)
        {
            if (dupamap[chuj2])
            {
                if (chuj2 >= BCONST || chuj2 <= -BCONST)
                {
                    ans2 -= vp.second * vp.second * dupamap[chuj2];
                    ans2 += (vp.second) * (vp.second - 1) * dupamap[chuj2];
                }
                else
                {
                    ans -= 3*vp.second * vp.second * dupamap[chuj2];
                    ans += 3*vp.second * (vp.second -1)* dupamap[chuj2];
                }
            }
        }
    }
    //assert(ans % 6 == 0);
    //assert(ans2 % 2 == 0);
    ans /= 6;
    ans += (ans2 / 2);
    sort(all(pos_big));
    sort(all(neg_big));
    int cnt = 1;
    vpii pos_big_cnt;
    vpii neg_big_cnt;
    for (int i = 1; i < sz(pos_big); i++)
    {
        if (pos_big[i - 1] != pos_big[i])
        {
            pos_big_cnt.push_back({pos_big[i - 1], cnt});
            cnt = 1;
        }
        else
        {
            cnt++;
        }
    }
    if (!pos_big.empty())
    {
        pos_big_cnt.push_back({pos_big.back(), cnt});
    }
    cnt = 1;
    for (int i = 1; i < sz(neg_big); i++)
    {
        if (neg_big[i - 1] != neg_big[i])
        {
            neg_big_cnt.push_back({neg_big[i - 1], cnt});
            cnt = 1;
        }
        else
        {
            cnt++;
        }
    }
    if (!neg_big.empty())
    {
        neg_big_cnt.push_back({neg_big.back(), cnt});
    }
    ll big_to_big = 0ll;
    for (const pii& s1p : pos_big_cnt)
    {
        for (const pii& s2p : neg_big_cnt)
        {
            int s3 = s1p.first + s2p.first;
            if (dupamap[-s3])
            {
                if (s3 >= BCONST || s3 <= -BCONST)
                {
                    if (-s3 == s1p.first)
                    {
                        ans += (s1p.second * (s1p.second - 1) / 2) * s2p.second;
                    }
                    else if (-s3 == s2p.first)
                    {
                        ans += (s2p.second * (s2p.second - 1) / 2) * s1p.second;
                    }
                    else
                    {
                        big_to_big += s1p.second * s2p.second * dupamap[-s3];
                    }
                }
                else
                {
                    ans += s1p.second * s2p.second * dupamap[-s3];
                }
            }
        }
    }
    big_to_big /= 2;
    ans += big_to_big;
    ll zeroans = 0ll;
    for (auto sp : sumsvp)
    {
        int cnt = sp.second;
        int v = sp.first;
        if (dupamap[-v])
        {
            zeroans += (cnt * dupamap[-v]) * zeros;
        }
    }
    ans += (zeroans/2);
    ans += (zeros * (zeros - 1) * (zeros - 2)) / 6;
    cout << ans << '\n';
}