#include <bits/stdc++.h>
using namespace std;
const long long N = 5 * 1e5 + 1;
long long n, a[N], sum[N], s, f, ans;
int32_t main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (i)
      sum[i] = sum[i - 1] + a[i];
    else
      sum[i] = a[i];
  }
  if (sum[n - 1] % 3) return cout << "0\n", 0;
  s = sum[n - 1] / 3;
  if (!s) {
    for (long long i = 0; i < n - 1; i++) {
      if (!sum[i]) f++;
    }
    cout << f * (f - 1) / 2 << '\n';
    return 0;
  }
  for (long long i = 0; i < n - 1; i++) {
    if (sum[i] == s)
      f++;
    else if (sum[i] == s * 2)
      ans += f;
  }
  cout << ans << '\n';
  return 0;
}
