#include <bits/stdc++.h>
using namespace std;
const int maxN = 5e5 + 5;
int n, a[maxN], cnt[maxN];
long long sum, s3, res;
int main() {
  ios_base::sync_with_stdio(false);
  scanf("%d\n", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    sum = sum + a[i];
  }
  if (sum % 3 != 0) {
    printf("%d\n", 0);
    return 0;
  }
  s3 = sum / 3;
  sum = 0;
  for (int i = n; i >= 1; --i) {
    cnt[i] = cnt[i + 1];
    sum = sum + a[i];
    if (sum == s3) cnt[i]++;
  }
  sum = 0;
  for (int i = 1; i <= n - 2; ++i) {
    sum = sum + a[i];
    if (sum == s3) res += cnt[i + 2];
  }
  cout << res;
  return 0;
}
