#include <bits/stdc++.h>
int main(void) {
  int i, j, k, n;
  scanf("%d", &n);
  long long arr[n + 2];
  for (i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
  }
  long long s = 0;
  for (i = 1; i <= n; i++) s += arr[i];
  long long sum = 0;
  int count[n + 2];
  memset(count, 0, sizeof(count));
  long long tmp = 0, c = 0;
  for (i = n; i >= 1; i--) {
    tmp += arr[i];
    if (tmp == s / 3) {
      c++;
    }
    count[i] = c;
  }
  long long ans = 0;
  if (s % 3 == 0) {
    for (i = 1; i <= n - 2; i++) {
      sum += arr[i];
      if (sum == s / 3) {
        ans += count[i + 2];
      }
    }
    printf("%lld", ans);
  } else {
    printf("0");
  }
  return 0;
}
