Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

const int MOD = 998244353;
ll facs[4005];
ll ifacs[4005];

ll choose(int n, int k) {
  ll ret = facs[n];
  ret *= ifacs[k];
  ret %= MOD;
  ret *= ifacs[n - k];
  ret %= MOD;
  return ret;
}

ll partition(int n, int k) { return choose(n + k - 1, n); }

ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while (e) {
    if (e % 2) {
      r *= b;
      r %= m;
    }
    e /= 2;
    if (e) {
      b *= b;
      b %= m;
    }
  }
  return r;
}

ll solve(int n, int k, int l) {
  ll ret = 0;
  for (int m = 0; m <= l; m++) {
    ll inc = choose(l, m);
    inc *= modpow(2, m, MOD);
    inc %= MOD;
    inc *= partition(n - m, k - 2 * l + m);
    inc %= MOD;
    ret += inc;
    ret %= MOD;
  }
  return ret;
}

void init() {
  facs[0] = 1;
  for (int i = 1; i <= 4000; i++) {
    facs[i] = facs[i - 1] * i;
    facs[i] %= MOD;
  }
  ifacs[4000] = modpow(facs[4000], MOD - 2, MOD);
  for (int i = 3999; i >= 0; i--) {
    ifacs[i] = ifacs[i + 1] * (i + 1);
    ifacs[i] %= MOD;
  }
}

void solve() {
  init();
  int n, k;
  cin >> k >> n;
  for (int s = 2; s <= 2 * k; s++) {
    int numpairs = 0;
    for (int i = 1; i <= k; i++) {
      int j = s - i;
      if (j <= i)
        break;
      if (j <= k)
        numpairs++;
    }
    ll ret = 0;
    if (s % 2 == 0) {
      ret += solve(n, k - 1, numpairs);
      ret += solve(n - 1, k - 1, numpairs);
      ret %= MOD;
    } else {
      ret += solve(n, k, numpairs);
    }
    cout << ret << "\n";
  }
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

const int MOD = 998244353;
ll facs[4005];
ll ifacs[4005];

ll choose(int n, int k) {
  ll ret = facs[n];
  ret *= ifacs[k];
  ret %= MOD;
  ret *= ifacs[n - k];
  ret %= MOD;
  return ret;
}

ll partition(int n, int k) {
  if (n < 0)
    return 0;
  return choose(n + k - 1, n);
}

ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while (e) {
    if (e % 2) {
      r *= b;
      r %= m;
    }
    e /= 2;
    if (e) {
      b *= b;
      b %= m;
    }
  }
  return r;
}

ll solve(int n, int k, int l) {
  ll ret = 0;
  for (int m = 0; m <= l; m++) {
    ll inc = choose(l, m);
    inc *= modpow(2, m, MOD);
    inc %= MOD;
    inc *= partition(n - m, k - 2 * l + m);
    inc %= MOD;
    ret += inc;
    ret %= MOD;
  }
  return ret;
}

void init() {
  facs[0] = 1;
  for (int i = 1; i <= 4000; i++) {
    facs[i] = facs[i - 1] * i;
    facs[i] %= MOD;
  }
  ifacs[4000] = modpow(facs[4000], MOD - 2, MOD);
  for (int i = 3999; i >= 0; i--) {
    ifacs[i] = ifacs[i + 1] * (i + 1);
    ifacs[i] %= MOD;
  }
}

void solve() {
  init();
  int n, k;
  cin >> k >> n;
  for (int s = 2; s <= 2 * k; s++) {
    int numpairs = 0;
    for (int i = 1; i <= k; i++) {
      int j = s - i;
      if (j <= i)
        break;
      if (j <= k)
        numpairs++;
    }
    ll ret = 0;
    if (s % 2 == 0) {
      ret += solve(n, k - 1, numpairs);
      ret += solve(n - 1, k - 1, numpairs);
      ret %= MOD;
    } else {
      ret += solve(n, k, numpairs);
    }
    cout << ret << "\n";
  }
}

void casesolve() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":"
         << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  // casesolve();
}