#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long arr[n];
  long long sum = 0;
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i];
    sum += arr[i];
  }
  long long cnt[n];
  memset(cnt, 0, sizeof cnt);
  if (sum % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  long long res = 0, foo = sum / 3;
  for (long long i = n - 1; i >= 0; --i) {
    res += arr[i];
    if (res == foo) {
      cnt[i] = 1;
    }
  }
  for (long long i = n - 2; i >= 0; --i) {
    cnt[i] += cnt[i + 1];
  }
  long long ans = 0, tot = 0;
  for (long long i = 0; i <= n - 3; ++i) {
    tot += arr[i];
    if (tot == foo) {
      ans += cnt[i + 2];
    }
  }
  cout << ans << endl;
  cout.flush();
}
