#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  long long A[N], suf[N], tot = 0, tmp, ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    tot += A[i];
  }
  if (tot % 3) {
    cout << "0\n";
  } else {
    tot /= 3;
    tmp = 0;
    for (int i = N - 1; i >= 0; i--) {
      tmp += A[i];
      suf[i] = (tmp == tot);
      if (i < N - 1) suf[i] += suf[i + 1];
    }
    tmp = 0;
    for (int i = 0; i < N - 2; i++) {
      tmp += A[i];
      if (tmp == tot) {
        ans += suf[i + 2];
      }
    }
    cout << ans << endl;
  }
}
