#include <bits/stdc++.h>
using namespace std;
long long dp1[5 * 100100];
long long a[5 * 100100];
int n;
int main() {
  cin >> n;
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", a + i);
    sum += a[i];
  }
  if (sum % (3LL) != 0 || n < 3) {
    printf("0\n");
    exit(0);
  }
  long long three = sum / (3LL);
  sum = 0;
  memset(dp1, 0, sizeof(dp1));
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    dp1[i + 1] += dp1[i];
    if (sum == three) dp1[i + 1] += 1;
  }
  sum = 0;
  long long ans = 0;
  for (int i = n; i >= 3; i--) {
    sum += a[i];
    if (sum == three) {
      ans += dp1[i - 1];
    }
  }
  cout << ans << endl;
}
