#include <bits/stdc++.h>
using namespace std;
long long arr[500005], cnt[500005];
int main() {
  int n;
  cin >> n;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
    sum += arr[i];
  }
  if (sum % 3)
    cout << 0 << endl;
  else {
    long long temp_sum = 0;
    for (int i = n - 1; i > 1; --i) {
      temp_sum = temp_sum + arr[i];
      if (temp_sum == sum / 3)
        cnt[i] = cnt[i + 1] + 1;
      else
        cnt[i] = cnt[i + 1];
    }
    temp_sum = 0;
    long long ans = 0;
    for (int i = 0; i < n - 2; ++i) {
      temp_sum += arr[i];
      if (temp_sum == sum / 3) {
        ans = ans + cnt[i + 2];
      }
    }
    cout << ans << endl;
  }
}
