Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end());
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int MOD = 1e9 + 7;
const int inv2 = 5e8 + 4;
const int maxn = 112345;

int fac[maxn * 2], ifac[maxn * 2];

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

inline int mul(int a, int b) { return ll(a) * b % MOD; }

int C(int n, int m) {
  if (m < 0 || m > n)
    return 0;
  return mul(mul(fac[n], ifac[m]), ifac[n - m]);
}

int pow_mod(int a, int k) {
  int ret = 1;
  while (k) {
    if (k & 1)
      ret = mul(ret, a);
    a = mul(a, a);
    k >>= 1;
  }
  return ret;
}

int inv(int a) { return pow_mod(a, MOD - 2); }

int b, w, dp[maxn][2];

int main() {
  scanf("%d%d", &b, &w);
  fac[0] = 1;
  FOR(i, 1, b + w) fac[i] = mul(fac[i - 1], i);
  REP(i, b + w + 1) ifac[i] = inv(fac[i]);
  FOR(i, 1, b + w) {
    dp[i][0] = add(mul(C(i - 1, i - b), pow_mod(inv2, i)), dp[i - 1][0]);
    dp[i][1] = add(mul(C(i - 1, i - w), pow_mod(inv2, i)), dp[i - 1][1]);
    int rem = add(add(1, MOD - dp[i - 1][0]), MOD - dp[i - 1][1]);
    printf("%d\n", add(mul(rem, inv2), dp[i - 1][1]));
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int(i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end());
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int MOD = 1e9 + 7;
const int inv2 = 5e8 + 4;
const int maxn = 112345;

int fac[maxn * 2], ifac[maxn * 2];

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

inline int mul(int a, int b) { return ll(a) * b % MOD; }

int C(int n, int m) {
  if (m < 0 || m > n)
    return 0;
  return mul(mul(fac[n], ifac[m]), ifac[n - m]);
}

int pow_mod(int a, int k) {
  int ret = 1;
  while (k) {
    if (k & 1)
      ret = mul(ret, a);
    a = mul(a, a);
    k >>= 1;
  }
  return ret;
}

int inv(int a) { return pow_mod(a, MOD - 2); }

int b, w, dp[maxn * 2][2];

int main() {
  scanf("%d%d", &b, &w);
  fac[0] = 1;
  FOR(i, 1, b + w) fac[i] = mul(fac[i - 1], i);
  REP(i, b + w + 1) ifac[i] = inv(fac[i]);
  FOR(i, 1, b + w) {
    dp[i][0] = add(mul(C(i - 1, i - b), pow_mod(inv2, i)), dp[i - 1][0]);
    dp[i][1] = add(mul(C(i - 1, i - w), pow_mod(inv2, i)), dp[i - 1][1]);
    int rem = add(add(1, MOD - dp[i - 1][0]), MOD - dp[i - 1][1]);
    printf("%d\n", add(mul(rem, inv2), dp[i - 1][1]));
  }
}