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