#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using pcc = pair<char, char>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vc = vector<char>;
using vd = vector<double>;
ll n, ara[500100], cnta[500100], cntb[500100], x = 0, y = 0, i, sum = 0, ans;
int main(int argc, char* argv[]) {
  scanf("%lld%*c", &(n));
  for (i = 0; i < n; i++) {
    scanf("%lld%*c", &(ara[i]));
    sum += ara[i];
  }
  if (sum == 0) {
    ll s = 0;
    for (i = 0; i < n; i++) {
      if (s == 0) x++;
      s += ara[i];
    }
    x = x - 2;
    ans = (x * (x + 1)) / 2;
  } else if (sum % 3 == 0) {
    sum /= 3;
    ll s = 0, a = 0, b = 0;
    for (i = 0; i < n; i++) {
      if (s == sum) {
        a++;
      } else if (s == sum * 2) {
        b++;
      }
      s += ara[i];
    }
    s = 0;
    for (i = 0; i < n - 1; i++) {
      if (s == sum) {
        x++;
        ans += (b);
      } else if (s == sum * 2) {
        b--;
      }
      s += ara[i];
    }
  } else
    ans = 0;
  printf("%lld", (ans));
  printf("\n");
  return 0;
}
