#include <bits/stdc++.h>
using namespace std;
void solve() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, a = 0;
  cin >> n;
  vector<long long> s, f, e;
  for (long long i = 0; i < n; i++) {
    long long c;
    cin >> c;
    a += c;
    s.push_back(a);
  }
  long long c = a / 3;
  if (a % 3) {
    cout << 0;
    return 0;
  }
  long long ans = 0, b = 0;
  for (long long i = 0; i < n - 1; i++) {
    if (s[i] == 2 * c) ans += b;
    if (s[i] == c) b++;
  }
  cout << ans;
  return 0;
}
