Problem p03083 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())

using namespace std;
using ll = int64_t;

constexpr int N = 1e5;
constexpr int MOD = 1e9 + 7;

ll factorials[N + 1];
pair<ll, ll> P[N + 1];

ll rev(ll x) {
  ll p = MOD - 2;
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    p >>= 1;
  }
  return ans;
}

pair<ll, ll> C(ll n, ll k) {
  return {factorials[n], factorials[k] * factorials[n - k] % MOD};
}

pair<ll, ll> M(pair<ll, ll> lhs, pair<ll, ll> rhs) {
  return {lhs.fi * rhs.fi % MOD, lhs.se * rhs.se % MOD};
}

ll A(pair<ll, ll> p) { return p.fi * rev(p.se) % MOD; }

pair<ll, ll> NEG(pair<ll, ll> p) { return {(MOD - p.fi) % MOD, p.se}; }

pair<ll, ll> PLUS(pair<ll, ll> lhs, pair<ll, ll> rhs) {
  return {(lhs.fi * rhs.se + rhs.fi * lhs.se) % MOD, (lhs.se * rhs.se) % MOD};
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  factorials[0] = factorials[1] = 1;
  for (int i = 2; i <= N; i++)
    factorials[i] = (factorials[i - 1] * i) % MOD;

  P[0] = {1, 1};
  for (int i = 1; i <= N; i++) {
    P[i] = M(P[i - 1], {1, 2});
  }

  int B, W;
  cin >> B >> W;

  pair<ll, ll> onlyBlackSum = {0, 1};
  pair<ll, ll> onlyWhiteSum = {0, 1};

  for (int i = 0; i < B + W; i++) {
    if (B <= i) {
      onlyWhiteSum = PLUS(
          onlyWhiteSum,
          M(M(C((i - 1), B - 1), M(P[B - 1], P[(i - 1) - (B - 1)])), {1, 2}));
    }
    if (W <= i) {
      onlyBlackSum = PLUS(
          onlyBlackSum,
          M(M(C((i - 1), W - 1), M(P[W - 1], P[(i - 1) - (W - 1)])), {1, 2}));
    }

    // cout << "sums: " << A(onlyWhiteSum) << " " << A(onlyBlackSum) << endl;

    ll ans = A(PLUS(
        M(PLUS(PLUS({1, 1}, NEG(onlyWhiteSum)), NEG(onlyBlackSum)), {1, 2}),
        onlyBlackSum));

    cout << ans << "\n";
  }

  return 0;
}


Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())

using namespace std;
using ll = int64_t;

constexpr int N = 3 * 1e5;
constexpr int MOD = 1e9 + 7;

ll factorials[N + 1];
pair<ll, ll> P[N + 1];

ll rev(ll x) {
  ll p = MOD - 2;
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    p >>= 1;
  }
  return ans;
}

pair<ll, ll> C(ll n, ll k) {
  return {factorials[n], factorials[k] * factorials[n - k] % MOD};
}

pair<ll, ll> M(pair<ll, ll> lhs, pair<ll, ll> rhs) {
  return {lhs.fi * rhs.fi % MOD, lhs.se * rhs.se % MOD};
}

ll A(pair<ll, ll> p) { return p.fi * rev(p.se) % MOD; }

pair<ll, ll> NEG(pair<ll, ll> p) { return {(MOD - p.fi) % MOD, p.se}; }

pair<ll, ll> PLUS(pair<ll, ll> lhs, pair<ll, ll> rhs) {
  return {(lhs.fi * rhs.se + rhs.fi * lhs.se) % MOD, (lhs.se * rhs.se) % MOD};
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  factorials[0] = factorials[1] = 1;
  for (int i = 2; i <= N; i++)
    factorials[i] = (factorials[i - 1] * i) % MOD;

  P[0] = {1, 1};
  for (int i = 1; i <= N; i++) {
    P[i] = M(P[i - 1], {1, 2});
  }

  int B, W;
  cin >> B >> W;

  pair<ll, ll> onlyBlackSum = {0, 1};
  pair<ll, ll> onlyWhiteSum = {0, 1};

  for (int i = 0; i < B + W; i++) {
    if (B <= i) {
      onlyWhiteSum = PLUS(
          onlyWhiteSum,
          M(M(C((i - 1), B - 1), M(P[B - 1], P[(i - 1) - (B - 1)])), {1, 2}));
    }
    if (W <= i) {
      onlyBlackSum = PLUS(
          onlyBlackSum,
          M(M(C((i - 1), W - 1), M(P[W - 1], P[(i - 1) - (W - 1)])), {1, 2}));
    }

    // cout << "sums: " << A(onlyWhiteSum) << " " << A(onlyBlackSum) << endl;

    ll ans = A(PLUS(
        M(PLUS(PLUS({1, 1}, NEG(onlyWhiteSum)), NEG(onlyBlackSum)), {1, 2}),
        onlyBlackSum));

    cout << ans << "\n";
  }

  return 0;
}