#include <bits/stdc++.h>
using namespace std;
int main() {
  long n;
  cin >> n;
  long long a = 0, b = 0;
  long long arr[n];
  for (long i = 0; i < n; i++) cin >> arr[i];
  long long sum[n];
  sum[0] = arr[0];
  for (long i = 1; i < n; i++) sum[i] = sum[i - 1] + arr[i];
  long long ful = sum[n - 1] / 3;
  if (sum[n - 1] % 3 != 0) {
    cout << "0";
    return 0;
  }
  long flag[n];
  long long temp = 0;
  for (long i = n - 1; i >= 0; i--) {
    temp += arr[i];
    flag[i] = 0;
    if (temp == ful) flag[i] = 1;
    if (i != n - 1) flag[i] = flag[i + 1] + flag[i];
  }
  long long res = 0;
  for (long i = 0; i < n - 2; i++) {
    if (sum[i] == ful) res += flag[i + 2];
  }
  cout << res;
}
