#include <bits/stdc++.h>
using namespace std;
void input(long long int *a, long long int n) {
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
}
long long int a[500001] = {0};
long long int c[500001] = {0};
long long int d[500001] = {0};
const long long int INF = 1e9;
void solve() {
  long long int n;
  cin >> n;
  input(a, n);
  c[0] = a[0];
  for (long long int i = 1; i < n; i++) c[i] = a[i] + c[i - 1];
  if (c[n - 1] % 3) {
    cout << 0;
    return;
  }
  long long int sum = 0;
  for (long long int i = n - 1; i >= 0; i--) {
    sum += a[i];
    if (sum == c[n - 1] / 3) {
      d[i] = 1;
    }
  }
  for (long long int i = n - 2; i >= 0; i--) {
    d[i] = d[i] + d[i + 1];
  }
  long long int ans = 0;
  for (long long int i = 0; i < n - 2; i++) {
    if (c[i] == c[n - 1] / 3) ans += d[i + 2];
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  for (long long int i = 0; i < t; i++) {
    solve();
  }
}
