#include <bits/stdc++.h>
using namespace std;
void fun() {
  long long n, i, x, ans = 0;
  ;
  cin >> n;
  long long sum[n];
  long long a[n];
  vector<long long> v;
  vector<long long> v2;
  cin >> a[0];
  sum[0] = a[0];
  for (i = 1; i < n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  if (sum[n - 1] % 3 != 0) {
    cout << "0";
    return;
  }
  for (i = 0; i < n - 1; i++) {
    if (sum[n - 1] / 3 == sum[i]) v.push_back(i);
    if (sum[i] % 2 == 0 && sum[n - 1] / 3 == sum[i] / 2) v2.push_back(i);
  }
  for (auto i : v) {
    x = v2.end() - upper_bound(v2.begin(), v2.end(), i);
    ans += x;
  }
  cout << ans << endl;
}
int main() { fun(); }
