#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long MOD = 998244353;
double eps = 1e-12;
int main() {
  int n;
  cin >> n;
  long long arr[n];
  long long sum[n];
  int cnt[n];
  int sums[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
    sum[i] = arr[i];
    if (i != 0) {
      sum[i] += sum[i - 1];
    }
  }
  if (n < 3 || sum[n - 1] % 3 != 0) {
    cout << 0 << "\n";
    return 0;
  }
  for (long long i = 0; i < n; i++) {
    long long num;
    if (i != 0)
      num = sum[n - 1] - sum[i - 1];
    else
      num = sum[n - 1];
    if (num == sum[n - 1] / 3) {
      cnt[i] = 1;
    } else
      cnt[i] = 0;
  }
  for (long long i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      sums[i] = cnt[i];
    else {
      sums[i] = sums[i + 1] + cnt[i];
    }
  }
  long long ways = 0;
  for (long long i = 0; i < n - 2; i++) {
    if (sum[i] == sum[n - 1] / 3) {
      ways += sums[i + 2];
    }
  }
  cout << ways << "\n";
}
