#include <bits/stdc++.h>
using namespace std;
int dcmp(double a, double b) {
  return fabs(a - b) <= 0.0000000001 ? 0 : (a > b) ? 1 : -1;
}
vector<long long> v;
long long arr[500000 + 9];
int main() {
  int n;
  cin >> n;
  long long sum = 0, no;
  for (int i = 0; i < n; i++) cin >> no, sum += no, v.push_back(no);
  if (sum % 3 != 0)
    cout << "0";
  else {
    sum /= 3;
    long long s = 0;
    for (int i = n - 1; i >= 0; i--) {
      s += v[i];
      if (s == sum) arr[i] = 1;
    }
    for (int i = n - 2; i >= 0; i--) arr[i] += arr[i + 1];
    s = 0;
    long long ans = 0;
    for (int i = 0; i < n - 2; i++) {
      s += v[i];
      if (s == sum) ans += arr[i + 2];
    }
    cout << ans;
  }
}
