#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  if (n < 3) {
    cout << "0";
    return 0;
  }
  long long s = 0;
  vector<long long> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    s += a[i];
  }
  if (s % 3 != 0) {
    cout << "0";
    return 0;
  }
  s /= 3;
  vector<long long> suf(n, 0);
  long long x = 0;
  for (int i = n - 1; i >= 0; --i) {
    x += a[i];
    if (x == s) ++suf[i];
    if (i != n - 1) suf[i] += suf[i + 1];
  }
  long long odp = 0;
  x = 0;
  for (int i = 0; i < n - 2; ++i) {
    x += a[i];
    if (x == s) odp += suf[i + 2];
  }
  cout << odp;
  return 0;
}
