#include <bits/stdc++.h>
using namespace std;
int _I() {
  int x;
  scanf("%d", &x);
  return x;
}
long long _L() {
  long long x;
  scanf("%lld", &x);
  return x;
}
long long gcd(long long x, long long y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}
long long lcm(long long x, long long y) {
  x /= gcd(x, y);
  return x * y;
}
int kdx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int kdy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int cases = 0;
long long ar[500007];
void lipu_mira() {
  long long i, j, k, n, ans = 0;
  n = _I();
  ar[0] = _L();
  for (i = 1; i < n; i++) {
    ar[i] = _L();
    ar[i] += ar[i - 1];
  }
  if (ar[n - 1] % 3 || n < 3) {
    puts("0");
    return;
  }
  if (ar[n - 1] == 0) {
    i = 0;
    while (ar[i]) {
      i++;
    }
    i++;
    for (; i < n - 1; i++) {
      if (ar[i] == 0) ans++;
    }
    ans = (ans * (ans + 1)) / 2;
    printf("%lld", ans);
    return;
  }
  long long v = ar[n - 1] / 3;
  long long v2 = 2 * v;
  long long xx = 0;
  bool f = 0;
  for (i = n - 1; i > -1; i--) {
    if (ar[i] == v) {
      ans += xx;
    } else if (ar[i] == v2) {
      xx++;
    }
  }
  printf("%lld", ans);
}
int main() {
  lipu_mira();
  return 0;
}
