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
// Author: Kajetan Ramsza
#include "bits/stdc++.h"
using namespace std;
#pragma GCC target ("avx")

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }

void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }

#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 
#endif

// Solution based on https://codeforces.com/blog/entry/117947

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 = MOD - 2; // pow(-MOD, -1, 2**32)
int m_reduce(long long x) {
    unsigned int m = (unsigned int) x * MODinv;
    return (x + (long long) m * MOD) >> 32;
}

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

int m_add(int x, int y) {
    x += y - 2 * MOD;
    return x >= 0 ? x : x + 2 * MOD;
}

int m_sub(int x, int y) {
    x -= y;
    return x >= 0 ? x : x + 2 * MOD;
}

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

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]);
    }

    for (int k = n; k > 1; k /= 2) {
        for (int i = 0; i < n/k; ++i) {
            int step = k * a;
            int half_step = step / 2;
            //int r = rt[i];
            int R = transformed_rt[i];
            for (int j1 = i * step; j1 < i * step + half_step; ++j1) {
                int j2 = j1 + half_step;
                int z = m_reduce((long long) R * P[j2]);
                P[j2] = m_sub(P[j1], z);
                P[j1] = m_add(P[j1], z);
            }
        }
    }

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

template<int a>
void inverse_transform(vector<int> &P) {
    int m = P.size();
    int n = m / a;
    int n_inv = 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] = (long long) n_inv * P[i] % MOD;
    
    // 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) {
            for (int k = 0; k < a; ++k)
                res[j + k] += (long long) P[i * a + j] * Q[i * a + k];
            if ((j & 7) == 7)
                for (int k = j; k < j + a; ++k)
                    if (res[k] >= 8 * MOD2) res[k] -= 8 * MOD2;
        }

        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);
}

const int maxs = 1e7 + 5;
const int lim = 50;
// const int lim = 5;
vi cnt(maxs, 0);
vi cnt_copy;
vi cnt2_mult;
vector<ll> cnt2(2*maxs, 0);

const int maxn = 507;

int n;
int arr[maxn];
int sum[maxn][maxn];
int mini = maxs;

ll val(ll s) {
    if(s-mini >= 0 && s-mini < maxs) 
        return (ll)cnt[s-mini];
    return 0;
} 

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

    cin>>n;
    rep(i,0,n) cin>>arr[i];

    rep(i,0,n) rep(j,i,n) {
        rep(k,i,j+1) sum[i][j] += arr[k];
        mini = min(mini, sum[i][j]);
    }
    dbg(mini);
    rep(i,0,n) rep(j,i,n) cnt[sum[i][j]-mini]++;

    cnt2_mult = cnt_copy = cnt;
    rep(s,0,maxs) if(cnt[s] > lim) {
        rep(i,0,n) rep(j,i,n)
            if(cnt2_mult[sum[i][j] - mini] > 0) 
                cnt2[s + sum[i][j] - mini] += (ll)cnt[s] * (1 + (sum[i][j] - mini != s));
        cnt2_mult[s] = cnt_copy[s] = 0;
    }

    fast_polymult(cnt2_mult, cnt_copy);
    rep(i,0,sz(cnt2_mult)) cnt2[i] += (ll)cnt2_mult[i];
    rep(i,0,2*maxs) {
        if(cnt2_mult[i] > 0) {
            dbg(i + 2*mini, cnt2_mult[i]);
        }
    }
    rep(i,0,maxs) cnt2[2*i] -= (ll)cnt[i];
    rep(i,0,maxs) if(cnt[i] > 0) dbg(i + mini, cnt[i]);
    rep(i,0,2*maxs) {
        cnt2[i] /= 2LL;
        if(cnt2[i] > 0) {
            // cerr<<i+2*mini<<' '<<cnt2[i]<<'\n';
            dbg(i + 2*mini, cnt2[i]);
        }
    }
    ll res = 0;
    rep(i,0,n) rep(j,i,n) {
        dbg(i,j, sum[i][j]);
        if(0 <= -sum[i][j] - 2 * mini && -sum[i][j] - 2 * mini < 2*maxs) {
            ll change = cnt2[-sum[i][j] - 2 * mini] - val(-2*sum[i][j]) + ll(sum[i][j] == 0);
            dbg(change);
            res += change;
        }
    }
    dbg(res);
    cout<<res/3<<'\n';
}