#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500 * 1000;
int n, a[MAXN + 5];
long long ans, s[MAXN + 5], s2[MAXN + 5];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  s[0] = a[0];
  for (int i = 1; i < n; i++) {
    s[i] = s[i - 1] + a[i];
  }
  if (s[n - 1] % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = (int)n - 3; i >= 0; i--)
    s2[i] = s2[i + 1] + (s[i + 1] == (2 * s[n - 1] / 3));
  for (int i = 0; i < n; i++)
    if (s[i] == (s[n - 1] / 3)) ans += s2[i];
  cout << ans << endl;
  return 0;
}
