#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long A[n], tot_sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> A[i];
    tot_sum += A[i];
  }
  if (tot_sum % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  bool B[n];
  fill(B, B + n, false);
  tot_sum /= 3;
  long long sum = 0;
  for (long long i = n - 1; i >= 0; i--) {
    sum += A[i];
    if (sum == tot_sum) {
      B[i] = true;
    }
  }
  long long count[n];
  count[n - 1] = B[n - 1];
  for (long long i = n - 2; i >= 0; i--) {
    count[i] = B[i] + count[i + 1];
  }
  sum = 0;
  long long unsigned lol = 0;
  for (long long i = 0; i + 2 < n; i++) {
    sum += A[i];
    if (tot_sum == sum) {
      lol += count[i + 2];
    }
  }
  cout << lol << endl;
  return 0;
}
