#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  long long a[n], sum = 0, sum1 = 0, sum2 = 0, mark1[n], mark2[n], k = 0, u = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum % 3 != 0)
    cout << "0\n";
  else {
    sum = sum / 3;
    for (long long i = 0; i < n; i++) {
      sum1 += a[i];
      if (sum1 == sum) mark1[k++] = i;
    }
    for (long long i = n - 1; i >= 0; i--) {
      sum2 += a[i];
      if (sum2 == sum) mark2[u++] = i;
    }
    long long ans = 0;
    sort(mark2, mark2 + u);
    for (long long i = 0; i < k; i++) {
      long long x = upper_bound(mark2, mark2 + u, mark1[i]) - mark2;
      long long y = u - 1 - x;
      if ((*upper_bound(mark2, mark2 + u, mark1[i])) != (mark1[i] + 1)) y += 1;
      ans += y;
    }
    cout << ans << endl;
  }
  return 0;
}
