#include <bits/stdc++.h>
using namespace std;
const int MX = 5e5 + 7;
long long a[MX], sum[MX], way[MX];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum[i] = sum[max(i - 1, 0)] + a[i];
  }
  if (sum[n - 1] % 3 != 0) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    way[i] = way[max(i - 1, 0)];
    if (sum[i] == sum[n - 1] / 3) way[i]++;
  }
  long long res = 0;
  for (int i = n - 1; i > 1; i--) {
    if (sum[n - 1] - sum[i - 1] == sum[n - 1] / 3) res += way[i - 2];
  }
  cout << '\n' << res;
  return 0;
}
