#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> suma(n + 1);
  suma[0] = 0;
  for (int i = 1; i < n + 1; ++i) {
    long long a;
    cin >> a;
    suma[i] = suma[i - 1] + a;
  }
  long long total = suma[n];
  if (total % 3 == 0 && n >= 3) {
    int i = 1;
    int j = 2;
    long long tercios = 0;
    long long dostercios = 0;
    while (j < n) {
      if (suma[i] == total / 3) {
        tercios++;
      }
      if (suma[j] == 2 * total / 3) {
        dostercios += tercios;
      }
      i++;
      j++;
    }
    cout << setprecision(20) << dostercios;
  } else
    cout << 0;
}
