#include <bits/stdc++.h>
using namespace std;
long long M = 1e9 + 7;
vector<long long> adj[100005];
long long p[100005];
long long d[100005];
map<pair<long long, long long>, long long> ma;
void f(long long i) {
  if (i != 1) f(p[i]);
  cout << i << " ";
}
void solve() {
  long long n;
  cin >> n;
  vector<long long> a(n, 0);
  long long sum = 0;
  for (int i = 0; i < n; i++) cin >> a[i], sum += a[i];
  vector<long long> b(n, 0), c(n, 0);
  if (sum % 3 != 0) {
    cout << "0";
    return;
  } else {
    sum /= 3;
    long long s = 0;
    for (int i = 0; i < n; i++) {
      s += a[i];
      if (s == sum) b[i] = 1;
    }
    s = 0;
    for (int i = n - 1; i >= 0; i--) {
      s += a[i];
      if (s == sum) c[i] = 1;
    }
    for (int i = n - 1 - 1; i >= 0; i--) {
      c[i] += c[i + 1];
    }
    long long ans = 0;
    for (int i = 0; i < n - 2; i++) {
      if (b[i] == 1) ans += c[i + 2];
    }
    cout << ans;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
