#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n];
  long long partial[n];
  vector<int> fronts, backs;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    partial[i] = a[i];
    if (i > 0) partial[i] += partial[i - 1];
  }
  if (partial[n - 1] % 3) {
    cout << 0 << endl;
    return 0;
  }
  long long sum = partial[n - 1] / 3;
  for (int i = 0; i < n; ++i) {
    if (partial[i] == sum) {
      fronts.emplace_back(i);
    }
    if (i < n - 1 && partial[n - 1] - partial[i] == sum) {
      backs.emplace_back(i + 1);
    }
  }
  long long result = 0;
  int last_checked = 0;
  for (const int i : fronts) {
    int j;
    for (j = last_checked; j < backs.size() && backs[j] <= i + 1; ++j)
      ;
    if (j == backs.size()) break;
    result += backs.size() - j;
    last_checked = j;
  }
  cout << result << endl;
  return 0;
}
