#include <bits/stdc++.h>
using namespace std;
long long n, sum = 0;
long long a[500002];
long long cnt[500002] = {0};
inline void ReadInput(void) {
  cin >> n;
  for (long long int i = (0); i < (n); i++) {
    cin >> a[i];
    sum = sum + a[i];
  }
}
long long second;
long long se = 0;
inline void solve(void) {
  if (sum % 3 != 0)
    cout << "0";
  else {
    second = sum / 3;
    for (long long i = n - 1; i >= 0; i--) {
      se += a[i];
      if (se == second) cnt[i] = 1;
    }
    for (long long i = n - 2; i >= 0; i--) {
      cnt[i] += cnt[i + 1];
    }
    long long re = 0, res = 0;
    for (long long i = 0; i + 2 < n; i++) {
      re = re + a[i];
      if (re == second) {
        res += cnt[i + 2];
      }
    }
    cout << res;
  }
}
inline void Refresh(void) {}
int main() {
  ReadInput();
  solve();
  return 0;
}
