#include <bits/stdc++.h>
using namespace std;
const int mod = 1e8;
int n, a[5 * 100001];
long long sum;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  if (n < 3 || sum % 3 != 0) cout << 0, exit(0);
  long long s1 = sum / 3, s2 = s1 * 2, ans = 0, k = 0;
  sum = 0;
  for (int i = 1; i < n; i++) {
    sum += a[i];
    if (sum == s2) {
      ans += k;
    }
    if (sum == s1) {
      k++;
    }
  }
  cout << ans;
}
