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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(ll i=(ll)a;i<=(ll)b;++i)
#define all(a) a.begin(),a.end()
#define sz(x) (int)(x).size()
const int mxN = 507, LIM = 2e7+7;
int p[mxN];

int m[2*LIM];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    rep(i,1,n) cin >> p[i];
    rep(i,2,n) p[i] += p[i-1];

    ll res = 0;
    rep(b,0,n){
        rep(e,0,n) rep(f,0,e-1) {
            res += m[LIM+p[b]+p[e]-p[f]];
        }
        rep(e,0,n) rep(f,0,e-1){
            m[LIM+p[b]+p[f]-p[e]]++;
        }
    }

     rep(b,0,n){
        rep(e,0,n) rep(f,0,e-1){
            m[LIM+p[b]+p[f]-p[e]]--;
        }
    }

    rep(a,0,n) rep(b,0,a-1) m[LIM+p[a]-p[b]]++;
    rep(a,0,n) rep(b,0,a-1) res -= 3*m[LIM+2*(p[b]-p[a])];

    rep(a,0,n) rep(b,0,a-1) m[LIM+p[a]-p[b]]--;

    rep(a,0,n){
        res += m[LIM+p[a]];
        m[LIM+p[a]] += 2;
    }

    cout << res/6 << "\n";
}