#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
long long a[N];
int main() {
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n;
  for (int j = 1; j <= n; j++) {
    cin >> a[j];
    a[j] += a[j - 1];
  }
  vector<int> v;
  long long k = 0, c = 0;
  if (a[n])
    for (int j = 1; j <= n; j++) {
      if ((a[n] - a[j - 1]) * 3 == a[n]) c += k;
      if (a[j] * 3 == a[n]) k++;
    }
  else {
    for (int j = 1; j <= n; j++) {
      if (a[j] == a[n]) k++;
    }
    c = (k - 2) * (k - 1) / 2;
  }
  cout << c << endl;
  return 0;
}
