Problem p03083 (C++)

Buggy submission (status: Runtime Error):
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
const int MAX = 110000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

std::unordered_map<ll, ll> f_memo;

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll mod = 1000000000 + 7;
// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll inv_mul(ll x, ll y) { return (x % mod * modpow(y, mod - 2, mod)) % mod; }
ll b, w;
ll f(ll i, ll c) {
  ll bw;
  if (c == b) {
    bw = 0;
  } else {
    bw = 1;
  }
  if (i == c + 1) {
    return inv_mul(COM(i - 2, c - 1), modpow(2, i - 1, mod));
  }
  if (f_memo[i * 10 + bw] != 0) {
    return f_memo[i * 10 + bw];
  } else {
    ll ans = f((i - 1), c) + inv_mul(COM(i - 2, c - 1), modpow(2, i - 1, mod));
    ans %= mod;
    return f_memo[i * 10 + bw] = ans;
  }
}

int main() {
  COMinit();
  std::cin >> b >> w;
  for (int i = 1; i <= b + w; i++) {
    ll result = 0;
    if (i <= b) {
      if (i <= w) {
        result = inv_mul(1, 2) % mod;
      } else {
        result = inv_mul(1, 2) % mod + inv_mul(1, 2) % mod * f(i, w) % mod;
      }
    } else {
      if (i <= w) {
        result = inv_mul(1, 2) % mod * ((1 - f(i, b) + mod) % mod) % mod;
      } else {
        result = inv_mul(1, 2) % mod * ((1 - f(i, b) + mod) % mod) % mod +
                 inv_mul(1, 2) % mod * f(i, w) % mod;
      }
    }
    std::cout << result % mod << std::endl;
  }
}


Fixed submission:
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
const int MAX = 210000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

std::unordered_map<ll, ll> f_memo;

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
ll mod = 1000000000 + 7;
// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll inv_mul(ll x, ll y) { return (x % mod * modpow(y, mod - 2, mod)) % mod; }
ll b, w;
ll f(ll i, ll c) {
  ll bw;
  if (c == b) {
    bw = 0;
  } else {
    bw = 1;
  }
  if (i == c + 1) {
    return inv_mul(COM(i - 2, c - 1), modpow(2, i - 1, mod));
  }
  if (f_memo[i * 10 + bw] != 0) {
    return f_memo[i * 10 + bw];
  } else {
    ll ans = f((i - 1), c) + inv_mul(COM(i - 2, c - 1), modpow(2, i - 1, mod));
    ans %= mod;
    return f_memo[i * 10 + bw] = ans;
  }
}

int main() {
  COMinit();
  std::cin >> b >> w;
  for (int i = 1; i <= b + w; i++) {
    ll result = 0;
    if (i <= b) {
      if (i <= w) {
        result = inv_mul(1, 2) % mod;
      } else {
        result = inv_mul(1, 2) % mod + inv_mul(1, 2) % mod * f(i, w) % mod;
      }
    } else {
      if (i <= w) {
        result = inv_mul(1, 2) % mod * ((1 - f(i, b) + mod) % mod) % mod;
      } else {
        result = inv_mul(1, 2) % mod * ((1 - f(i, b) + mod) % mod) % mod +
                 inv_mul(1, 2) % mod * f(i, w) % mod;
      }
    }
    std::cout << result % mod << std::endl;
  }
}