#include <bits/stdc++.h>
using namespace std;
long long a[5 * 100100];
long long ans, num, sum, sum1, three;
int n;
int main() {
  cin >> n;
  sum = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", a + i);
    sum += a[i];
  }
  if (sum % (3LL) != 0 || n < 3) {
    cout << 0 << endl;
    exit(0);
  }
  three = sum / 3;
  ans = num = sum1 = 0;
  for (int i = 1; i <= n - 1; i++) {
    sum1 += a[i];
    if (sum1 == 2 * three && i >= 2) ans += num;
    if (sum1 == three) num++;
  }
  cout << ans << endl;
}
