#include <bits/stdc++.h>
using namespace std;
long long int a[500005];
long long int wh[500005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, i, j, k, s1 = 0, s2 = 0, ways = 0;
  cin >> n;
  long long int a[n + 1], wh[n + 1];
  memset(wh, 0, sizeof(wh));
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s1 += a[i];
  }
  if (s1 % 3 != 0)
    cout << "0";
  else {
    s1 /= 3;
    for (i = n - 1; i >= 0; i--) {
      s2 += a[i];
      if (s2 == s1) wh[i] = 1;
    }
    for (i = n - 2; i >= 0; i--) wh[i] += wh[i + 1];
    s2 = 0;
    for (i = 0; i + 2 < n; i++) {
      s2 += a[i];
      if (s2 == s1) ways += wh[i + 2];
    }
    cout << ways;
  }
  return 0;
}
