#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a, ans = 0;
  scanf("%lld", &n);
  vector<long long> vec, v1, v2;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a);
    vec.push_back(a);
    if (i) vec[i] += vec[i - 1];
  }
  if (vec[n - 1] % 3 == 0) {
    long long one = vec[n - 1] / 3, two = one * 2;
    for (int i = 0; i < n - 1; i++) {
      if (vec[i] == one) v1.push_back(i);
      if (vec[i] == two) v2.push_back(i);
    }
    for (int i = 0; i < v1.size(); i++) {
      long long lb = lower_bound(v2.begin(), v2.end(), v1[i] + 1) - v2.begin();
      ans += ((long long)v2.size() - lb);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
