#include <bits/stdc++.h>
using namespace std;
int i, n;
long long a[500005], nr, x;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 2; i <= n; i++) a[i] += a[i - 1];
  for (i = 1; i <= n - 1; i++) {
    if (a[i] * 3 == a[n] * 2) nr += x;
    if (a[i] * 3 == a[n]) x++;
  }
  cout << nr << endl;
  return 0;
}
