#include <bits/stdc++.h>
using namespace std;
int a[500010];
long long f[500010];
int main() {
  int n;
  scanf("%d", &n);
  long long k = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    k += a[i];
  }
  if (k % 3) {
    printf("0");
  } else {
    k /= 3;
    long long sum = 0, ans;
    f[n] = 0;
    for (int i = n - 1; i >= 0; --i) {
      sum += a[i];
      f[i] = (sum == k) + f[i + 1];
    }
    sum = 0, ans = 0;
    for (int i = 0; i < n - 2; ++i) {
      sum += a[i];
      ans += (sum == k) * f[i + 2];
    }
    printf("%I64d", ans);
  }
}
