#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  scanf("%d", &n);
  long a[n];
  long long s = 0, ss = 0;
  for (int i = 0; i < n; i++) {
    scanf("%ld", &a[i]);
    s += a[i];
  }
  if (s % 3 != 0) {
    printf("0\n");
    return 0;
  }
  s = s / 3;
  vector<int> sum1;
  vector<int> sum2;
  for (int i = 0; i < n - 1; i++) {
    ss += a[i];
    if (ss == s) sum1.push_back(i);
    if (ss == 2 * s) sum2.push_back(i);
  }
  long long ans = 0;
  for (int i = 0; i < sum1.size(); i++) {
    ans += sum2.end() - lower_bound(sum2.begin(), sum2.end(), sum1[i] + 1);
  }
  printf("%I64d\n", ans);
  return 0;
}
