#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000001;
long long a[maxn], b[maxn], x[maxn];
int n;
int main() {
  long long ans = 0;
  int i;
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  memset(x, 0, sizeof(x));
  scanf("%I64d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    b[i] = b[i - 1] + a[i];
  }
  for (i = n; i >= 1; i--) {
    if (b[n] - b[i - 1] == b[n] / 3)
      x[i] = x[i + 1] + 1;
    else
      x[i] = x[i + 1];
  }
  for (i = 1; i <= n; i++)
    if (b[i] == b[n] / 3) ans += x[i + 2];
  if (b[n] % 3 != 0 || n <= 2) ans = 0;
  printf("%I64d\n", ans);
  return 0;
}
