#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<long long> a(n + 1, 0);
  long long s1 = 0, s2 = 0, s3 = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] += a[i - 1];
  }
  if (a[n] % 3) {
    cout << "0\n";
    return;
  }
  vector<int> a0, a1;
  for (int i = 1; i < n; i++) {
    if (a[i] == a[n] / 3) a0.push_back(i);
    if (a[i] == 2 * a[n] / 3) a1.push_back(i);
  }
  long long ret = 0;
  while (a0.size() && a1.size()) {
    while (a0.size() && a0.back() >= a1.back()) {
      a0.pop_back();
    }
    if (!a0.size()) break;
    ret += a0.size();
    a1.pop_back();
  }
  cout << ret << endl;
}
int main() {
  solve();
  return 0;
}
