#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int n;
  long long int ans = 0, sum = 0;
  cin >> n;
  vector<long long int> a(n), b(n, 0), c(n, 0), pref(n, 0);
  pref[0] = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    pref[i + 1] = a[i] + pref[i];
  }
  if (pref[n] % 3 != 0) {
    cout << "0\n";
    return 0;
  }
  sum = pref[n] / 3;
  long long int s = 0;
  for (int i = n - 1; i >= 0; i--) {
    s += a[i];
    if (s == sum) c[i] = 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    c[i] += c[i + 1];
  }
  for (int i = 0; i < n - 2; i++) {
    if (pref[i + 1] == sum) {
      ans += c[i + 2];
    }
  }
  cout << ans << '\n';
  return 0;
}
