Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
// #include <fstream>
#define typeof(x) __typeof__(x)
#define int long long int
#define mod(x) ((x % MOD) + MOD) % MOD
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) (int)((c).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort(ALL(c))
#define LB(c, x) (int)(lower_bound(ALL(c), x) - (c).begin())
#define UB(c, x) (int)(upper_bound(ALL(c), x) - (c).begin())
#define COUNT(c, x) (int)(UB(c, x) - LB(c, x))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using namespace std;

typedef pair<int, int> P;
struct edge {
  int to, cost;
};

const int INF = 1e18;
const int MOD = 998244353;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}

int frac[(int)1e7 + 1];
void init_frac(int N) {
  frac[0] = 1;
  for (int i = 1; i <= N; i++) {
    frac[i] = mod(frac[i - 1] * i);
  }
}

// int powM(int x, int n){
//   if(n == 0){
//     return 1;
//   }
//   if(n % 2 == 0){
//     return powM(mod(x * x), n / 2);
//   }
//   return mod(powM(x, n-1) * x);
// }
int powM(int x, int n) {
  int res = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      res = mod(res * x);
    }
    x = mod(x * x);
    n >>= 1;
  }
  return res;
}

int inverse(int x) { return powM(x, MOD - 2); }

int comb(int n, int r) {
  if (r < 0) {
    return 0;
  }
  return mod(frac[n] * inverse(mod(frac[n - r] * frac[r])));
}

signed main() {
  // string filename; cin >> filename;
  // ifstream in(filename);
  // cin.rdbuf(in.rdbuf());

  int K, N;
  cin >> K >> N;
  init_frac(1e7);

  rep(i, 2, 2 * K + 1) {
    if (i % 2 == 1) {
      int idx = min(i, 2 * K + 2 - i);
      int n = (idx - 1) / 2;
      int s = 0;
      rep(j, 0, n + 1) {
        s = mod(s + mod(comb(n, j) * mod(powM(2, j) * comb(N - j + K - idx + j,
                                                           K - idx + j))));
      }
      printf("%lld\n", s);
    } else {
      int idx = min(i, 2 * K + 2 - i);
      int n = idx / 2 - 1;
      int s = 0;
      rep(j, 0, n + 1) {
        s = mod(s + mod(comb(n, j) *
                        mod(powM(2, j) *
                            mod(comb(N - j + K - idx + j, K - idx + j) +
                                comb(N - j - 1 + K - idx + j, K - idx + j)))));
      }
      printf("%lld\n", s);
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
// #include <fstream>
#define typeof(x) __typeof__(x)
#define int long long int
#define mod(x) ((x % MOD) + MOD) % MOD
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define ALL(c) (c).begin(), (c).end()
#define SIZE(c) (int)((c).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort(ALL(c))
#define LB(c, x) (int)(lower_bound(ALL(c), x) - (c).begin())
#define UB(c, x) (int)(upper_bound(ALL(c), x) - (c).begin())
#define COUNT(c, x) (int)(UB(c, x) - LB(c, x))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using namespace std;

typedef pair<int, int> P;
struct edge {
  int to, cost;
};

const int INF = 1e18;
const int MOD = 998244353;

template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}

int frac[(int)1e7 + 1];
void init_frac(int N) {
  frac[0] = 1;
  for (int i = 1; i <= N; i++) {
    frac[i] = mod(frac[i - 1] * i);
  }
}

// int powM(int x, int n){
//   if(n == 0){
//     return 1;
//   }
//   if(n % 2 == 0){
//     return powM(mod(x * x), n / 2);
//   }
//   return mod(powM(x, n-1) * x);
// }
int powM(int x, int n) {
  int res = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      res = mod(res * x);
    }
    x = mod(x * x);
    n >>= 1;
  }
  return res;
}

int inverse(int x) { return powM(x, MOD - 2); }

int comb(int n, int r) {
  if (n < 0 || n - r < 0 || r < 0) {
    return 0;
  }
  return mod(frac[n] * inverse(mod(frac[n - r] * frac[r])));
}

signed main() {
  // string filename; cin >> filename;
  // ifstream in(filename);
  // cin.rdbuf(in.rdbuf());

  int K, N;
  cin >> K >> N;
  init_frac(1e7);

  rep(i, 2, 2 * K + 1) {
    if (i % 2 == 1) {
      int idx = min(i, 2 * K + 2 - i);
      int n = (idx - 1) / 2;
      int s = 0;
      rep(j, 0, n + 1) {
        s = mod(s + mod(comb(n, j) * mod(powM(2, j) * comb(N - j + K - idx + j,
                                                           K - idx + j))));
      }
      printf("%lld\n", s);
    } else {
      int idx = min(i, 2 * K + 2 - i);
      int n = idx / 2 - 1;
      int s = 0;
      rep(j, 0, n + 1) {
        s = mod(s + mod(comb(n, j) *
                        mod(powM(2, j) *
                            mod(comb(N - j + K - idx + j, K - idx + j) +
                                comb(N - j - 1 + K - idx + j, K - idx + j)))));
      }
      printf("%lld\n", s);
    }
  }
  return 0;
}