#include <bits/stdc++.h>
using namespace std;
int n, m, k, l;
int a[511111];
long long sum;
int cnt[511111];
int sums[511111];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum % 3) {
    cout << 0;
  } else {
    sum /= 3;
    long long ss = 0;
    for (int i = n - 1; i >= 0; --i) {
      ss += a[i];
      if (ss == sum) cnt[i] = 1;
    }
    for (int i = n - 2; i >= 0; --i) cnt[i] += cnt[i + 1];
    long long ans = 0;
    ss = 0;
    for (int i = 0; i + 2 < n; ++i) {
      ss += a[i];
      if (ss == sum) ans += cnt[i + 2];
    }
    cout << ans << "\n";
  }
  return 0;
}
