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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<queue>
#include<stack>
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
typedef vector<LD> VLD;
typedef vector<VLL > VVLL;
typedef vector<VLD > VVLD;
typedef vector<PII > VPII;
typedef __int128 int128;
template<class TIn>
using indexed_set = tree<
        TIn, null_type, less<TIn>,
        rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;

#define FOR(x, b, e) for(int x=b; x<=(e); ++x)
#define FORD(x, b, e) for(int x=b; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define THIS (*this)
#define LSB(x) (x & -x)
#define SQR(x) ((x)*(x))

typedef complex<LD> CLD;
typedef vector<CLD> VCLD;
const LD PI = acos(-1.0);

inline void sortByRevBit(VCLD &a) {
    for (int i = 1, j = 0; i < a.size(); i++) {
        int bit = a.size() >> 1;
        for (; j & bit; bit >>= 1) j ^= bit;
        j ^= bit;
        if (i < j) swap(a[i], a[j]);
    }
}

void fft(VCLD & a, bool invert) {
    int n = a.size();

    sortByRevBit(a);

    for (int len = 2; len <= n; len <<= 1) {
        LD ang = 2 * PI / len * (invert ? -1 : 1);
        CLD wlen(cos(ang), sin(ang));
        for (int i = 0; i < n; i += len) {
            CLD w(1);
            for (int j = 0; j < len / 2; j++) {
                CLD 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 (CLD & x : a)
            x /= n;
    }
}

VLL multiply(VLL &a, VLL &b) {
    int n = 1; while (n < a.size() + b.size() - 1) n <<= 1;
    VCLD ca(a.begin(), a.end()), cb(b.begin(), b.end());
    ca.resize(n); cb.resize(n);
    fft(ca, false); fft(cb, false);
    VCLD cc(n);
    REP(k, n) cc[k] = ca[k] * cb[k];
    fft(cc, true);
    VLL res(n);
    REP(i, n) res[i] = round(cc[i].real());
    res.resize(a.size() + b.size() - 1);
    return res;
}

VLL square(VLL &a) {
    int n = 1; while (n < 2 * a.size() - 1) n <<= 1;
    VCLD ca(a.begin(), a.end());
    ca.resize(n); 
    fft(ca, false);
    VCLD cc(n);
    REP(k, n) cc[k] = ca[k] * ca[k];
    fft(cc, true);
    VLL res(n);
    res.resize(2 * ca.size() - 1);
    REP(i, res.size()) res[i] = round(cc[i].real());
    return res;
}

class SumArr {
    private:
    VI sums;

    public:
    SumArr(VI &a): sums(a.size() + 1) {
        REP(i, a.size()) sums[i + 1] = sums[i] + a[i];
    }
    int sum(int i, int j) { return sums[j + 1] - sums[i]; }
};

int main() {
	ios_base::sync_with_stdio(0);
    cin.tie(NULL); cout.tie(NULL);
    int n; cin >> n;
    VI a(n);
    REP(i, n) cin >> a[i];
    int shift = 0, maxVal = -1e9;
    VI b;
    SumArr sums(a);
    REP(i, n) FOR(j, i, n - 1) {
        b.PB(sums.sum(i, j));
        shift = max(shift, -b.back());
        maxVal = max(maxVal, b.back());
    }
    //REP(i, b.size()) printf("%d ", b[i]); printf("\n");
    VLL f(shift + maxVal + 1);
    LL zeros = 0;
    REP(i, b.size()) {
        if (b[i] == 0) zeros++;
        else f[b[i] + shift]++;
    }
    VLL ff = multiply(f, f);
    REP(i, b.size()) if (b[i] != 0) ff[2 * (b[i] + shift)] -= 1;
    //REP(i, ff.size()) printf("%lld ", ff[i]); printf("\n");
    LL ans = 0; LL red = 0;
    //unordered_set<LL> processed;
    REP(i, b.size()) if (b[i] != 0) {
        if (-b[i] >= -2 * shift && -b[i] <= 2 * maxVal) {
            ans += ff[-b[i] + 2 * shift] / 2;
            //printf("before: %lld\n", ans);
            if (2 * -b[i] >= -shift && 2 * -b[i] <= maxVal) ans -= f[2 * -b[i] + shift];
            //processed.insert(b[i]);
        }
        //printf("after: %lld\n", ans);
    }
    ans /= 3;
    ans -= red;
    ans += zeros * (zeros - 1) * (zeros - 2) / 6;
    ans += ff[2 * shift] / 2 * zeros;
    cout << ans << '\n';

    return 0;
}