#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t n;
  cin >> n;
  int64_t *a = new int64_t[n + 1];
  int64_t sum = 0;
  for (int64_t i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (sum % 3 != 0) {
    cout << 0;
    return 0;
  }
  int64_t *dp = new int64_t[n + 2];
  int64_t current = 0;
  for (int64_t i = n - 1; i >= 0; i--) {
    current += a[i];
    if (current == sum / 3) {
      dp[i] = 1;
    } else {
      dp[i] = 0;
    }
  }
  for (int64_t i = n - 2; i >= 0; i--) {
    dp[i] += dp[i + 1];
  }
  current = 0;
  int64_t res = 0;
  for (int64_t i = 0; i < n - 2; i++) {
    current += a[i];
    if (current == sum / 3) res += dp[i + 2];
  }
  cout << res;
}
