Problem p03270 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

const lint mod = 998244353;
lint emod(lint a) { return (a % mod + mod) % mod; }

lint powm(lint a, lint n) {
  return n ? powm(a * a % mod, n >> 1) * (n & 1 ? a : 1) % mod : 1;
}

lint invm(lint a) {
  a %= mod;
  return a == 1 ? 1 : -mod / a * invm(mod % a) % mod;
}

unordered_map<lint, unordered_map<lint, lint>> _com;
lint com(lint n, lint r) {
  r = min(r, n - r);
  if (_com[n].count(r))
    return _com[n][r];
  return _com[n][r] = r ? n * invm(r) % mod * com(n - 1, r - 1) % mod : 1;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  lint k, n;
  cin >> k >> n;
  V<lint> res(k);
  for (lint i = 2; i < k + 1; i++) {
    if (i & 1) {
      lint m = (i - 1) / 2;
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 1, k - 2 * m + j - 1)) %
            mod;
      }
    } else {
      lint m = (i - 2) / 2;
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 2, k - 2 * m + j - 2)) %
            mod;
      }
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 3, k - 2 * m + j - 2)) %
            mod;
      }
    }
  }
  lint i = k + 1;
  if (i & 1) {
    lint m = (i - 1) / 2;
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 1, k - 2 * m + j - 1)) %
          mod;
    }
  } else {
    lint m = (i - 2) / 2;
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 2, k - 2 * m + j - 2)) %
          mod;
    }
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 3, k - 2 * m + j - 2)) %
          mod;
    }
  }
  for (int i = 0; i < k; i++)
    cout << emod(res[i]) << '\n';
  for (int i = k - 2; i >= 0; i--)
    cout << emod(res[i]) << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T> void assign(V<T> &v, int n, const T &a = T()) {
  v.assign(n, a);
}
template <class T, class... U> void assign(V<T> &v, int n, const U &...u) {
  v.resize(n);
  for (auto &&i : v)
    assign(i, u...);
}

const lint mod = 998244353;
lint emod(lint a) { return (a % mod + mod) % mod; }

lint powm(lint a, lint n) {
  return n ? powm(a * a % mod, n >> 1) * (n & 1 ? a : 1) % mod : 1;
}

lint invm(lint a) {
  a %= mod;
  return a == 1 ? 1 : -mod / a * invm(mod % a) % mod;
}

unordered_map<lint, unordered_map<lint, lint>> _com;
lint com(lint n, lint r) {
  if (n < 0 or r < 0 or r > n)
    return 0;
  r = min(r, n - r);
  if (_com[n].count(r))
    return _com[n][r];
  return _com[n][r] = r ? n * invm(r) % mod * com(n - 1, r - 1) % mod : 1;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  lint k, n;
  cin >> k >> n;
  V<lint> res(k);
  for (lint i = 2; i < k + 1; i++) {
    if (i & 1) {
      lint m = (i - 1) / 2;
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 1, k - 2 * m + j - 1)) %
            mod;
      }
    } else {
      lint m = (i - 2) / 2;
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 2, k - 2 * m + j - 2)) %
            mod;
      }
      for (lint j = 0; j < m + 1; j++) {
        res[i - 2] =
            (res[i - 2] + com(m, j) * powm(2, j) % mod *
                              com(n + k - 2 * m - 3, k - 2 * m + j - 2)) %
            mod;
      }
    }
  }
  lint i = k + 1;
  if (i & 1) {
    lint m = (i - 1) / 2;
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 1, k - 2 * m + j - 1)) %
          mod;
    }
  } else {
    lint m = (i - 2) / 2;
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 2, k - 2 * m + j - 2)) %
          mod;
    }
    for (lint j = 1; j < m + 1; j++) {
      res[i - 2] =
          (res[i - 2] + com(m, j) * powm(2, j) % mod *
                            com(n + k - 2 * m - 3, k - 2 * m + j - 2)) %
          mod;
    }
  }
  for (int i = 0; i < k; i++)
    cout << emod(res[i]) << '\n';
  for (int i = k - 2; i >= 0; i--)
    cout << emod(res[i]) << '\n';
}