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