#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> in(n), prefix(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
    prefix[i] = (i != 0 ? prefix[i - 1] : 0) + in[i];
  }
  if (n < 3)
    cout << "0" << endl;
  else {
    long long ans = 0;
    unordered_map<long long, long long> f;
    f[prefix[n - 1]]++;
    f[prefix[n - 2]]++;
    for (int i = n - 3; i >= 0; i--) {
      if (prefix[i] * 3 == prefix[n - 1]) {
        f[2 * prefix[i]] += 0;
        if (prefix[i] == 0)
          ans += f[prefix[i]] - 1;
        else
          ans += f[2 * prefix[i]];
      }
      f[prefix[i]]++;
    }
    cout << ans << endl;
  }
}
