#include <bits/stdc++.h>
using namespace std;
long long x, n, i, j, k, l, a[500007], sum, cnt;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1];
  for (i = 1; i < n; i++) {
    if (a[i] * 3 == a[n] * 2) cnt = cnt + x;
    if (a[i] * 3 == a[n]) x++;
  }
  cout << cnt;
}
