#include <bits/stdc++.h>
using namespace std;
long long a[500005], flag, ans, cnt;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = a[i] + a[i - 1];
  }
  for (int i = 1; i < n; i++) {
    if (a[i] * 3 == a[n] * 2) ans += cnt;
    if (a[i] * 3 == a[n]) cnt++;
  }
  cout << ans << endl;
  return 0;
}
