#include <bits/stdc++.h>
using namespace std;
long long n, ans, ps[500001];
int dp[500002];
int arr[500001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  long long sum = 0, s = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    s += arr[i];
    ps[i] = ps[i - 1] + arr[i];
  }
  if (s % 3) return cout << 0 << endl, 0;
  long long third = s / 3;
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1];
    sum += arr[n - i + 1];
    if (sum == third) dp[i]++;
  }
  for (int i = 1; i <= n - 2; i++) {
    if (ps[i] == third) {
      ans += dp[n - i - 1];
    }
  }
  cout << ans << endl;
}
