#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
long long pre[maxn], suf[maxn], sum, a[maxn];
long long pre1[maxn], suf1[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (register int i = (1); i <= (n); i++) {
    cin >> a[i];
    pre[i] = pre[i - 1] + a[i];
  }
  sum = pre[n];
  if (sum % 3 || n < 3) {
    cout << 0 << endl;
    return 0;
  }
  sum /= 3;
  int cnt = 0;
  for (register int i = (1); i <= (n); i++) {
    if (pre[i] == sum)
      pre1[i] = ++cnt;
    else
      pre1[i] = pre1[i - 1];
  }
  cnt = 0;
  for (register int i = (n); i >= (1); i--) {
    suf[i] = suf[i + 1] + a[i];
    if (suf[i] == sum)
      suf1[i] = ++cnt;
    else
      suf1[i] = suf1[i + 1];
  }
  long long ans = 0;
  for (register int i = (1); i <= (n); i++) {
    if (pre[i] == sum) ans += suf1[i + 2];
  }
  cout << ans << endl;
  return 0;
}
