#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long n, c, z = 0, y = 0;
  cin >> n;
  long long a[n], b[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
    if (i > 0) b[i] += b[i - 1];
  }
  if (b[n - 1] % 3 || n < 3) {
    cout << 0;
    return 0;
  }
  c = b[n - 1] / 3;
  for (long long i = 0; i < n - 1; i++) {
    if (b[i] == 2 * c) y += z;
    if (b[i] == c) z++;
  }
  cout << y;
}
