#include <bits/stdc++.h>
using namespace std;
long long n;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  vector<long long> x, y;
  cin >> n;
  vector<long long> a(n + 1);
  long long s = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    s += a[i];
  }
  if (s % 3) return cout << 0, 0;
  long long v = 0;
  for (long long i = 1; i <= n; i++) {
    v += a[i];
    if (v == s / 3) x.push_back(i);
    if (v == 2 * s / 3) y.push_back(i);
  }
  sort((x).begin(), (x).end());
  sort((y).begin(), (y).end());
  long long ans = 0;
  long long flag = (find((y).begin(), (y).end(), n) == y.end()) ? 0 : 1;
  for (auto t : x) {
    if (t == n) continue;
    auto it = upper_bound((y).begin(), (y).end(), t);
    if (it == y.end() || *it == n) continue;
    long long cnt = it - y.begin();
    cnt = (long long)y.size() - cnt;
    ans += cnt - flag;
  }
  cout << ans << endl;
  return 0;
}
