#include <bits/stdc++.h>
using namespace std;
int a[500010];
long long sum[500010];
int indx[500010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (i == 0)
      sum[i] = a[i];
    else
      sum[i] = sum[i - 1] + a[i];
  }
  if (sum[n - 1] % 3 != 0 || n < 3)
    cout << "0" << endl;
  else {
    long long mid = sum[n - 1] / 3, ans = 0;
    int k = 0;
    for (int i = 0; i < n; i++) {
      if (sum[i] == 2 * mid) indx[k++] = i;
    }
    if (k != 0 && indx[k - 1] == n - 1) k--;
    for (int i = 0; i < n; i++) {
      if (sum[i] == mid) {
        int high = upper_bound(indx, indx + k, i) - indx;
        if (high < k && i != indx[high] && indx[high] != n - 1 && i != n - 1)
          ans += k - high;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
