#include <bits/stdc++.h>
using namespace std;
int n, a[1000000], x, y, dp[1000000];
long long ans = 0, sum;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum % 3 != 0) {
    cout << "0" << endl;
    return 0;
  }
  long long s = 0;
  sum = sum / 3;
  for (int i = 1; i <= n; i++) {
    s += a[i];
    if (s == sum) {
      dp[i] = 1 + dp[i - 1];
    } else
      dp[i] = dp[i - 1];
  }
  s = 0;
  for (int i = n; i > 2; i--) {
    s += a[i];
    if (s == sum) ans += dp[i - 2];
  }
  cout << ans << endl;
  return 0;
}
