#include <bits/stdc++.h>
using namespace std;
long long s1[500001], a[500001];
long long s2[500001], work[500001];
int main() {
  int n, i, j, k;
  long long c = 0, sum = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    s1[i] = a[i] + s1[i - 1];
    sum += a[i];
  }
  if (sum % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  for (i = n; i >= 1; i--) {
    s2[i] = a[i] + s2[i + 1];
    if (s2[i] == sum / 3)
      work[i] = 1 + work[i + 1];
    else
      work[i] = work[i + 1];
  }
  for (i = 1; i <= n; i++) {
    if (s1[i] == sum / 3) c += work[i + 2];
  }
  cout << c << endl;
  return 0;
}
