#include <bits/stdc++.h>
using namespace std;
int n;
long long a[500005];
long long pre[500005];
int vis1[500005], vis2[500005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    pre[i] = pre[i - 1] + a[i];
  }
  long long sum = pre[n];
  if (sum % 3) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (pre[i] == sum / 3) vis1[i] = 1;
    if (pre[i] == sum / 3 * 2) vis2[i] = 1;
  }
  int num = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (vis2[i]) ans += num;
    if (vis1[i]) num++;
  }
  printf("%I64d\n", ans);
  return 0;
}
