#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, k;
  int n;
  cin >> n;
  vector<long long> arr(n);
  long long sum = 0;
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  if (sum % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n < 3) {
    cout << 0 << endl;
    return 0;
  }
  long long s = sum / 3;
  vector<int> ans(n, 0);
  long long pref = 0;
  for (i = n - 1; i >= 0; i--) {
    pref += arr[i];
    if (pref == s) {
      ans[i] = 1;
    }
  }
  for (i = n - 2; i >= 0; i--) {
    ans[i] += ans[i + 1];
  }
  pref = 0;
  long long soln = 0;
  for (i = 0; i < n - 2; i++) {
    pref += arr[i];
    if (pref == s) {
      soln += ans[i + 2];
    }
  }
  cout << soln << endl;
  return 0;
}
