#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
std::istream& operator>>(std::istream& i, pair<T, U>& p) {
  i >> p.x >> p.y;
  return i;
}
template <typename T>
std::istream& operator>>(std::istream& i, vector<T>& t) {
  for (auto& v : t) {
    i >> v;
  }
  return i;
}
template <typename T, typename U>
std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) {
  o << p.x << ' ' << p.y;
  return o;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const vector<T>& t) {
  if (t.empty()) o << '\n';
  for (size_t i = 0; i < t.size(); ++i) {
    o << t[i] << " \n"[i == t.size() - 1];
  }
  return o;
}
void solve() {
  int n;
  cin >> n;
  vector<long long> A(n), pr(n, 0);
  long long tot = 0;
  for (int i = 0; i < n; ++i) cin >> A[i], tot += A[i];
  if (tot % 3) {
    cout << 0;
    return;
  }
  tot /= 3;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += A[i];
    if (sum == tot) ++pr[i];
  }
  for (int i = 1; i < n; ++i) pr[i] += pr[i - 1];
  sum = 0;
  long long ans = 0;
  for (int i = n - 1; i > 1; --i) {
    sum += A[i];
    if (sum == tot) ans += pr[i - 2];
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
