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
#include <bits/stdc++.h>

using namespace std;

struct Complex {
    double real, imag;
    Complex() : real(0), imag(0) {}
    Complex(double _real, double _imag) : real(_real), imag(_imag) {}
    Complex(double _angle) {
        real = cos(_angle);
        imag = sin(_angle);
    }
};

Complex operator* (const Complex a, const Complex b) {
    return Complex(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
}

Complex operator+ (const Complex a, const Complex b) {
    return Complex(a.real + b.real, a.imag + b.imag);
}

Complex operator- (const Complex a, const Complex b) {
    return Complex(a.real - b.real, a.imag - b.imag);
}

void to_second(vector<Complex>&poly) {
    int n = 31 - __builtin_clz(poly.size());
    
    for (int i = 1, j = 0; i < (1 << n); i++) {
        int bit = 1 << (n - 1);
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(poly[i], poly[j]);
    }
    
    for(int len = 2; len <= (1 << n); len *= 2) {
        double angle = 2 * M_PI / len;
        Complex step(angle);
        for(int k = 0; k < (1 << n); k += len) {
            Complex current(1.0, 0.0);
            for(int i = 0; i < len / 2; i++) {
                Complex a = poly[k + i], b = poly[k + i + len / 2] * current;
                
                poly[k + i] = a + b;
                poly[k + i + len / 2] = a - b;
                
                current = current * step;
            }
        }
    }
    
    for(int i = 0; i < (1 << n); i++)
        poly[i] = poly[i] * poly[i];
    
    for (int i = 1, j = 0; i < (1 << n); i++) {
        int bit = 1 << (n - 1);
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(poly[i], poly[j]);
    }
    
    for(int len = 2; len <= (1 << n); len *= 2) {
        double angle = -2 * M_PI / len;
        Complex step(angle);
        for(int k = 0; k < (1 << n); k += len) {
            Complex current(1.0, 0.0);
            for(int i = 0; i < len / 2; i++) {
                Complex a = poly[k + i], b = poly[k + i + len / 2] * current;
                
                poly[k + i] = a + b;
                poly[k + i + len / 2] = a - b;
                
                current = current * step;
            }
        }
    }
    
    for(auto &z : poly) {
        z.real /= (1 << n);
        z.imag /= (1 << n);
    }
}

long long calc_sub(vector<int>&a) {
    // First count all equal
    
    int n = a.size();
    vector<int>p(n + 1);
    for(int i = 1; i <= n; i++)
        p[i] = p[i - 1] + a[i - 1];
    
    long long result = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < i; j++) {
            if(p[i] == p[j]) {
                result++;
            }
        }
    }

    map<int, int>cnt;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < i; j++) {
            cnt[p[i] - p[j]]++;
        }
    }

    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < i; j++) {
            int my_sum = p[i] - p[j];
            result += 3 * cnt[-2 * my_sum];
            if(my_sum == 0)
                result -= 3;
        }
    }
    return result;
}

int main() {
    ios_base::sync_with_stdio(0);
    
    vector<Complex>m;
    
    int n; cin >> n;
    vector<int>a(n);
    for(auto &x : a)
        cin >> x;
    
    vector<int>b;
    for(int i = 0; i < n; i++) {
        int sum = 0;
        for(int j = i; j < n; j++) {
            sum += a[j];
            b.push_back(sum);
        }
    }
    
    sort(b.begin(), b.end());
/*    
    cout << "b = ";
    for(int x : b)
        cout << x << " ";
    cout << "\n";*/
    
    int mn = *min_element(b.begin(), b.end());
    int mx = *max_element(b.begin(), b.end());
    
    
    vector<Complex>poly(mx - mn + 1);
    while(__builtin_popcount(poly.size()) != 1)
        poly.emplace_back();
    
    
    while(poly.size() < 2 * (mx - mn + 1))
        poly.resize(poly.size() * 2);
    
    for(int x : b) {
        poly[x - mn].real += 1.0;
    }
    
    vector<Complex>poly0 = poly;
    
    to_second(poly);
    
//     cout << "poly: ";
//     for(auto z : poly)
//         cout << round(z.real) << " ";
//     cout << "\n";
//     
//     cout << "poly0: ";
//     for(auto z : poly0)
//         cout << round(z.real) << " ";
//     cout << "\n";
    
    long long sub = calc_sub(a);
    
    long long result = 0;
    for(int i = 0; i < (int)poly0.size(); i++) {
        //i + j == -mn * 3
        int j = -mn * 3 - i;
        if(j >= 0 && j < (int)poly.size())
            result += (long long)round(poly0[i].real) * (long long)round(poly[j].real);
    }
//     cout << "result = " << result << "\n";
//     cout << "sub = " << sub << "\n";
    //   cout << "sub = " << sub << ", result = " << result << "\n";
    cout << (result - sub) / 6 << "\n";
    
    
    return 0;
}