#include <bits/stdc++.h>
using namespace std;
signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long a[n], s = 0;
  for (long long i = 0; i < n; ++i) {
    cin >> a[i];
    s += a[i];
  }
  if (s % 3 != 0) {
    cout << 0;
    return 0;
  }
  vector<long long> first, second;
  long long c = 0;
  for (auto i = 0; (0 <= n && i < n) || (0 > n && i > n);
       i += (0 <= n) ? 1 : -1) {
    c += a[i];
    if (c == s / 3) first.push_back(i);
  }
  c = 0;
  for (auto i = n - 1; (n - 1 <= -1 && i < -1) || (n - 1 > -1 && i > -1);
       i += (n - 1 <= -1) ? 1 : -1) {
    c += a[i];
    if (c == s / 3) second.push_back(i - 1);
  }
  sort((second).begin(), (second).end());
  long long ans = 0;
  for (auto i : first) {
    long long pf =
        upper_bound((second).begin(), (second).end(), i) - second.begin();
    pf = second.size() - pf;
    ans += pf;
  }
  cout << ans;
}
