#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
signed main() {
  long long n;
  cin >> n;
  vector<long long> a(n + 1);
  for (long long i = 1; i <= n; i++) cin >> a[i];
  long long s = 0;
  for (long long i = 1; i <= n; i++) s += a[i];
  if (s % 3 != 0) {
    cout << 0;
    return 0;
  }
  vector<long long> sum(n + 2);
  vector<long long> p;
  for (long long i = n; i >= 1; i--) {
    sum[i] = sum[i + 1] + a[i];
    if (sum[i] == s / 3) p.push_back(i);
  }
  vector<long long> ss(n + 1);
  for (long long i = 1; i <= n; i++) ss[i] = ss[i - 1] + a[i];
  reverse(p.begin(), p.end());
  long long x = 0;
  long long ans = 0;
  for (long long i = 2; i <= n; i++) {
    if (ss[i - 1] == s / 3) {
      long long po = lower_bound(p.begin(), p.end(), i + 1) - p.begin();
      ans += p.size() - po;
    }
  }
  cout << ans << "\n";
  return 0;
}
