#include <bits/stdc++.h>
using namespace std;
int n, a[500001];
long long ps[500001];
map<long long, int> ps_count;
int main() {
  scanf("%d", &n);
  ps[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ps[i] = ps[i - 1] + a[i];
    ps_count[ps[i]]++;
  }
  ps_count[ps[n]]--;
  long long ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    ps_count[ps[i]]--;
    if (ps[i] * 3 == ps[n]) ans = ans + ps_count[ps[i] * 2];
  }
  printf("%lld\n", ans);
  return 0;
}
