#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
int n;
int a[500005];
long long sum[500005], ans;
int lcnt[500005], rcnt[500005];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    sum[i] = sum[i - 1] + a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (sum[i] * 3 == sum[n]) lcnt[i]++;
    lcnt[i] += lcnt[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    if ((sum[n] - sum[i - 1]) * 3 == sum[n]) {
      ans += lcnt[i - 2];
    }
  }
  printf("%lld", ans);
  return 0;
}
