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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
namespace lc {
template <int MOD> class ModulusInteger {
public:
  typedef ModulusInteger<MOD> self_type;

private:
  int m_value;
  static self_type unsafe_construct(int x) {
    self_type y;
    y.m_value = x;
    return y;
  }

public:
  ModulusInteger() : m_value(0) {}
  ModulusInteger(int x) : m_value(x % MOD) {
    if (m_value < 0) {
      m_value += MOD;
    }
  }
  int operator*() const { return m_value; }
  self_type &operator=(const self_type &x) {
    m_value = x.m_value;
    return *this;
  }
  self_type operator+(const self_type &x) const {
    const int y = m_value + x.m_value;
    return unsafe_construct(y >= MOD ? y - MOD : y);
  }
  self_type operator-(const self_type &x) const {
    const int y = m_value - x.m_value;
    return unsafe_construct(y < 0 ? y + MOD : y);
  }
  self_type operator*(const self_type &x) const {
    return unsafe_construct(static_cast<long long>(m_value) * x.m_value % MOD);
  }
  self_type &operator+=(const self_type &x) { return (*this = *this + x); }
  self_type &operator-=(const self_type &x) { return (*this = *this - x); }
  self_type &operator*=(const self_type &x) { return (*this = *this * x); }
};
template <int MOD>
std::ostream &operator<<(std::ostream &os, const ModulusInteger<MOD> &x) {
  os << *x;
  return os;
}
} // namespace lc
using namespace std;
typedef lc::ModulusInteger<1000000007> mint;
static mint dp[5001][5001];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, k, q;
  cin >> n >> m >> k >> q;
  dp[0][0] = 1;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < k; ++j) {
      const mint t = dp[i][j];
      dp[i + 1][j] += t;
      dp[i + 1][j + k] -= t;
    }
    for (int j = k; j < k + k; ++j) {
      const mint t = dp[i][j];
      dp[i + 1][j] += t;
      dp[i + 1][k + k] -= t;
      dp[i + 1][0] += t;
      dp[i + 1][j - k] -= t;
    }
    for (int j = 0; j < k + k; ++j) {
      dp[i + 1][j + 1] += dp[i + 1][j];
    }
  }
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    if (b == 0) {
      cout << (a == c ? 1 : 0) << endl;
    } else {
      a %= (2 * k);
      c %= (2 * k);
      auto answer = dp[b][(c - a + 2 * k) % (2 * k)];
      if (b > 1) {
        mint x = n / (k * 2), z = 1;
        --b;
        while (b > 0) {
          if (b & 1) {
            z *= x;
          }
          x *= x;
          b >>= 1;
        }
        answer *= z;
      }
      cout << answer << endl;
    }
  }
  return 0;
}

Error: 127
/tmp/268ca1df-c68f-4496-a356-13e1ae93c308.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
namespace lc {
template <int MOD> class ModulusInteger {
public:
  typedef ModulusInteger<MOD> self_type;

private:
  int m_value;
  static self_type unsafe_construct(int x) {
    self_type y;
    y.m_value = x;
    return y;
  }

public:
  ModulusInteger() : m_value(0) {}
  ModulusInteger(int x) : m_value(x % MOD) {
    if (m_value < 0) {
      m_value += MOD;
    }
  }
  int operator*() const { return m_value; }
  self_type &operator=(const self_type &x) {
    m_value = x.m_value;
    return *this;
  }
  self_type operator+(const self_type &x) const {
    const int y = m_value + x.m_value;
    return unsafe_construct(y >= MOD ? y - MOD : y);
  }
  self_type operator-(const self_type &x) const {
    const int y = m_value - x.m_value;
    return unsafe_construct(y < 0 ? y + MOD : y);
  }
  self_type operator*(const self_type &x) const {
    return unsafe_construct(static_cast<long long>(m_value) * x.m_value % MOD);
  }
  self_type &operator+=(const self_type &x) { return (*this = *this + x); }
  self_type &operator-=(const self_type &x) { return (*this = *this - x); }
};
template <int MOD>
std::ostream &operator<<(std::ostream &os, const ModulusInteger<MOD> &x) {
  os << *x;
  return os;
}
} // namespace lc
using namespace std;
typedef lc::ModulusInteger<1000000007> mint;
static mint dp[5001][5001];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m, k, q;
  cin >> n >> m >> k >> q;
  const int c = n / (k * 2);
  dp[0][0] = 1;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < k; ++j) {
      const mint t = dp[i][j] * c;
      dp[i + 1][j] += t;
      dp[i + 1][j + k] -= t;
    }
    for (int j = k; j < k + k; ++j) {
      const mint t = dp[i][j] * c;
      dp[i + 1][j] += t;
      dp[i + 1][k + k] -= t;
      dp[i + 1][0] += t;
      dp[i + 1][j - k] -= t;
    }
    for (int j = 0; j < k + k; ++j) {
      dp[i + 1][j + 1] += dp[i + 1][j];
    }
  }
  for (int i = 0; i < q; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    const auto x = dp[b - 1][(c - a + 2 * k) % (2 * k)];
    cout << x << endl;
  }
  return 0;
}