Problem p02611 (C++) — buggy submission, verdict: Runtime Error

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

vector<vector<long long int>> matrix_product(vector<vector<long long int>> v,
                                             vector<vector<long long int>> w) {
  vector<vector<long long int>> ret(v.size(),
                                    vector<long long int>(w.front().size()));
  for (int i = 0; i < ret.size(); i++) {
    for (int j = 0; j < ret[i].size(); j++) {
      for (int k = 0; k < w.size(); k++) {
        ret[i][j] += v[i][k] * w[k][j];
        ret[i][j] %= MOD;
      }
    }
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> K;
  while (K--) {
    cin >> N;
    vector<vector<long long int>> box(21, vector<long long int>(21));
    for (int i = 0; i < 10; i++) {
      box[i][i] = box[i][i + 1] = 1;
    }
    box[10][12] = 1;
    box[10][10] = 1;
    for (int i = 11; i <= 19; i += 2) {
      box[i][i + 1] = 1;
      box[i + 1][i] = 1;
      if (i + 3 <= 20)
        box[i + 1][i + 3] = 1;
    }
    vector<vector<long long int>> dp(21, vector<long long int>(1));
    dp[20][0] = 1;
    N += 10;
    auto by = box;
    for (int i = 0; i <= 30; i++) {
      if ((N >> i) & 1) {
        dp = matrix_product(by, dp);
      }
      by = matrix_product(by, by);
    }
    cout << dp.front().front() << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

long long int power(long long int x, long long int n, long long int M) {
  long long int ret = 1;
  long long int by = x;
  while (n) {
    if (n & 1) {
      ret *= by;
      ret %= M;
    }
    by *= by;
    by %= M;
    n >>= 1;
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> K;
  while (K--) {
    cin >> N;
    vector<long long int> by(N + 5, 1);
    for (int i = 2; i <= N + 4; i++) {
      by[i] = by[i - 1] * i % MOD;
    }
    long long int ans = 0;
    for (int i = 5; i <= N; i++) {
      ans += by[(N - i) / 2 + 5] * power(by[5], MOD - 2, MOD) % MOD *
             power(by[(N - i) / 2], MOD - 2, MOD) % MOD * by[i + 4] *
             power(by[i - 5], MOD - 2, MOD) % MOD * power(by[9], MOD - 2, MOD) %
             MOD;
    }
    ans %= MOD;
    cout << ans << endl;
  }
}