#include <bits/stdc++.h>
using namespace std;
long long n, sum = 0;
long long pre[1000000], two[1000000];
void _pre() {
  for (long long i = n - 2; i >= 0; i--) {
    if (pre[i] == 2 * sum)
      two[i] = two[i + 1] + 1;
    else
      two[i] = two[i + 1];
  }
}
int main() {
  scanf("%lld", &n);
  ;
  for (long long i = 0; i < n; i++) {
    long long t;
    scanf("%lld", &t);
    ;
    sum += t;
    pre[i] = sum;
  }
  if (sum % 3 != 0 or n < 3) {
    cout << 0;
    return 0;
  }
  sum /= 3;
  _pre();
  long long ans = 0;
  for (long long i = 0; i < n - 2; i++)
    if (pre[i] == sum) ans += two[i + 1];
  cout << ans;
  return 0;
}
