Problem p03877 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define N 40010
ll n, a, b, c;

ll inv[N], fact[N], ifact[N], be[N];

ll comb(ll a, ll b) { return fact[a + b] * ifact[a] % mod * ifact[b] % mod; }

ll comb_nk(ll n, ll k) {
  if (n < 0)
    return 1;
  return comb(n - k, k);
}

ll pow_mod(ll a, ll r, ll m) {
  ll x = 1;
  while (r) {
    if (r & 1)
      (x *= a) %= m;
    (a *= a) %= m;
    r >>= 1;
  }
  return x;
}

void init_fact(ll n = N) {
  inv[1] = 1;
  for (int i = 2; i < n; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  fact[0] = ifact[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    ifact[i] = (ifact[i - 1] * inv[i]) % mod;
  }
}

int main() {
  init_fact();
  cin >> n >> a >> b >> c;
  if (b % 2) {
    cout << 0 << endl;
    return 0;
  }
  b /= 2;
  ll res = 0;
  rep(y, a + 1) {
    ll z = a - y;
    for (ll x = 0; x <= (c - y) / 3; x++) {
      ll w = c - y - 3 * x;
      if (w < 0)
        continue;
      if (b == 0 && w > 0)
        continue;
      (res += fact[x + y + z + b] * ifact[x] % mod * ifact[y] % mod * ifact[z] %
              mod * ifact[b] % mod * comb_nk(w + b - 1, w)) %= mod;
    }
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define rep(i, n) for (ll(i) = 0; (i) < (ll)(n); ++(i))
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) (x = min(x, y))
#define chmax(x, y) (x = max(x, y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}
#define N 40010
ll n, a, b, c;

ll inv[N], fact[N], ifact[N], be[N];

ll comb(ll a, ll b) { return fact[a + b] * ifact[a] % mod * ifact[b] % mod; }

ll comb_nk(ll n, ll k) { return comb(n - k, k); }

ll pow_mod(ll a, ll r, ll m) {
  ll x = 1;
  while (r) {
    if (r & 1)
      (x *= a) %= m;
    (a *= a) %= m;
    r >>= 1;
  }
  return x;
}

void init_fact(ll n = N) {
  inv[1] = 1;
  for (int i = 2; i < n; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  fact[0] = ifact[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    ifact[i] = (ifact[i - 1] * inv[i]) % mod;
  }
}

int main() {
  init_fact();
  cin >> n >> a >> b >> c;
  if (b % 2) {
    cout << 0 << endl;
    while (true)
      ;
    return 0;
  }
  if (b == 0)
    return -1;
  b /= 2;
  ll res = 0;
  rep(y, a + 1) {
    ll z = a - y;
    for (ll x = 0; x <= (c - y) / 3; x++) {
      ll w = c - y - 3 * x;
      if (b == 0 && w > 0)
        continue;
      (res += fact[x + y + z + b] * ifact[x] % mod * ifact[y] % mod * ifact[z] %
              mod * ifact[b] % mod * comb_nk(w + b - 1, w)) %= mod;
    }
  }
  cout << res << endl;
  return 0;
}