#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, ans = 0, cnt = 0;
  cin >> n;
  vector<long long> a(n), pref(n + 1);
  for (auto& i : a) cin >> i;
  pref[0] = a[0];
  for (int i = 1; i < n; ++i) pref[i] += pref[i - 1] + a[i];
  if (pref[n - 1] % 3 || n < 3) return cout << 0, 0;
  long long s = pref[n - 1] / 3;
  for (int i = 0; i < n - 1; ++i) {
    if (pref[i] == 2 * s) ans += cnt;
    if (pref[i] == s) ++cnt;
  }
  cout << ans << '\n';
}
