#include <bits/stdc++.h>
using namespace std;
long long la[500005], ra[500005], a[500005];
int cnt[500005];
int main() {
  int n;
  long long sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    la[i] = a[i] + la[i - 1];
  }
  int l = 1, r = n;
  if (n <= 2) {
    cout << 0;
    return 0;
  }
  long long total = 0;
  if (sum % 3 != 0) {
    cout << 0;
    return 0;
  }
  sum /= 3;
  long long way = 0;
  for (int i = 1; i <= n; i++) {
    if (sum == la[i]) cnt[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = n; i > 2; i--) {
    total += a[i];
    if (total == sum) {
      way += cnt[i - 2];
    }
  }
  cout << way;
  return 0;
}
