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

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const long long MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <i64 mod = MOD> struct ModInt {
  i64 p;

  ModInt() : p(0) {}
  ModInt(i64 x) { p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod; }

  ModInt &operator+=(const ModInt &y) {
    p = p + *y - ((p + *y) >= mod ? mod : 0);
    return *this;
  }
  ModInt &operator-=(const ModInt &y) {
    p = p - *y + (p - *y < 0 ? mod : 0);
    return *this;
  }
  ModInt &operator*=(const ModInt &y) {
    p = (p * *y) % mod;
    return *this;
  }
  ModInt &operator%=(const ModInt &y) {
    if (y)
      p %= *y;
    return *this;
  }

  ModInt operator+(const ModInt &y) const {
    ModInt x = *this;
    return x += y;
  }
  ModInt operator-(const ModInt &y) const {
    ModInt x = *this;
    return x -= y;
  }
  ModInt operator*(const ModInt &y) const {
    ModInt x = *this;
    return x *= y;
  }
  ModInt operator%(const ModInt &y) const {
    ModInt x = *this;
    return x %= y;
  }

  friend ostream &operator<<(ostream &stream, const ModInt<mod> &x) {
    stream << *x;
    return stream;
  }

  friend ostream &operator>>(ostream &stream, const ModInt<mod> &x) {
    stream >> *x;
    return stream;
  }

  ModInt &operator++() {
    p = (p + 1) % mod;
    return *this;
  }
  ModInt &operator--() {
    p = (p - 1 + mod) % mod;
    return *this;
  }

  bool operator==(const ModInt &y) const { return p == *y; }
  bool operator!=(const ModInt &y) const { return p != *y; }

  const i64 &operator*() const { return p; }
  i64 &operator*() { return p; }
};

using mint = ModInt<>;

template <typename T> struct Matrix {
  int h, w;
  vector<T> v;

  Matrix() : h(1), w(1), v(1, 1) {}
  Matrix(int n) { *this = makeUnit(n); }
  Matrix(int h, int w) : h(h), w(w), v(h * w, 0) {}

  Matrix(vector<vector<T>> v_) : h(v_.size()), w(v_[0].size()), v(h * w) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        v[i * w + j] = v_[i][j];
  }

  static Matrix makeUnit(int n) {
    Matrix mat(n, n);
    for (int i = 0; i < n; ++i)
      mat.at(i, i) = 1;
    return mat;
  }

  T &at(int i, int j) {
    assert(0 <= i && i <= h && 0 <= j && j < w);
    return v[i * w + j];
  };

  Matrix pow(i64 x) {
    assert(h == w);
    auto mat = x & 1 ? *this : makeUnit(h);
    auto u = *this;
    while (u = u * u, x >>= 1)
      if (x & 1)
        mat *= u;
    return mat;
  }

  Matrix &operator+=(const Matrix &mat) {
    assert(h == mat.h && w == mat.w);
    for (int i = 0; i < h * w; ++i)
      v[i] += mat.v[i];
    return *this;
  }

  Matrix &operator-=(const Matrix &mat) {
    assert(h == mat.h && w == mat.w);
    for (int i = 0; i < h * w; ++i)
      v[i] -= mat.v[i];
    return *this;
  }

  Matrix &operator%=(const T mod) {
    for (int i = 0; i < h * w; ++i)
      v[i] %= mod;
    return *this;
  }

  Matrix operator*(const Matrix &mat) {
    assert(w == mat.h);
    Matrix ret(h, mat.w);
    for (int i = 0; i < h; ++i)
      for (int k = 0; k < w; ++k)
        for (int j = 0; j < mat.w; ++j)
          ret.v[i * mat.w + j] += v[i * w + k] * mat.v[k * mat.w + j];
    return ret;
  }

  Matrix operator+(const Matrix &mat) { return Matrix(*this) += mat; }
  Matrix operator-(const Matrix &mat) { return Matrix(*this) -= mat; }
  Matrix operator%(const T mod) { return Matrix(*this) %= mod; }
  Matrix &operator*=(const Matrix &mat) { return *this = *this * mat; }
};

void solve(long long k, std::string s) {
  int n = s.size();
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    v[i] = s[i] - 'a';

  auto mpow = [](auto x, i64 y) {
    auto z = x;
    decltype(x) val = y & 1 ? x : decltype(x)(1);
    while (z *= z, y >>= 1)
      if (y & 1)
        val *= z;
    return val;
  };

  vector<mint> tf(n + k + 10, 1);
  for (int i = 0; i < n + k + 9; ++i)
    tf[i + 1] = tf[i] * 25;
  vector<mint> ts(n + k + 10, 1);
  for (int i = 0; i < n + k + 9; ++i)
    ts[i + 1] = ts[i] * 26;

  auto calc_fact = [mpow] {
    constexpr int N = 3e6;
    vector<mint> fact(N + 1, 1);
    vector<mint> inv(N + 1, 1);
    for (int i = 1; i < N; ++i) {
      fact[i + 1] = fact[i] * (i + 1);
      inv[i + 1] = mpow(fact[i + 1], MOD - 2);
    }
    return make_pair(fact, inv);
  };
  vector<mint> fact, inv;
  tie(fact, inv) = calc_fact();

  auto ncr = [&fact, &inv](int n, int r) {
    if (n < 0 || r < 0 || n < r)
      return mint(0);
    return fact[n] * inv[r] * inv[n - r];
  };

  // 1文字マッチしてない状態で見る
  // i: マッチ文字数 j: s[i]の位置
  int i = n - 1;
  // int j = n + k;
  mint ans = 0;
  for (int j = n - 1; j <= n + k - 1; ++j) {
    int p1 = j;
    int p2 = j - i;
    mint val = tf[p2] * ncr(p1, p2);
    ans += val * ts[n + k - 1 - j];
  }
  cout << ans << endl;
}

signed main() {
  long long K;
  scanf("%lld", &K);
  std::string S;
  std::cin >> S;
  solve(K, S);
  return 0;
}


Error: 0

Fixed submission:

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const long long MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

template <i64 mod = MOD> struct ModInt {
  i64 p;

  ModInt() : p(0) {}
  ModInt(i64 x) { p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod; }

  ModInt &operator+=(const ModInt &y) {
    p = p + *y - ((p + *y) >= mod ? mod : 0);
    return *this;
  }
  ModInt &operator-=(const ModInt &y) {
    p = p - *y + (p - *y < 0 ? mod : 0);
    return *this;
  }
  ModInt &operator*=(const ModInt &y) {
    p = (p * *y) % mod;
    return *this;
  }
  ModInt &operator%=(const ModInt &y) {
    if (y)
      p %= *y;
    return *this;
  }

  ModInt operator+(const ModInt &y) const {
    ModInt x = *this;
    return x += y;
  }
  ModInt operator-(const ModInt &y) const {
    ModInt x = *this;
    return x -= y;
  }
  ModInt operator*(const ModInt &y) const {
    ModInt x = *this;
    return x *= y;
  }
  ModInt operator%(const ModInt &y) const {
    ModInt x = *this;
    return x %= y;
  }

  friend ostream &operator<<(ostream &stream, const ModInt<mod> &x) {
    stream << *x;
    return stream;
  }

  friend ostream &operator>>(ostream &stream, const ModInt<mod> &x) {
    stream >> *x;
    return stream;
  }

  ModInt &operator++() {
    p = (p + 1) % mod;
    return *this;
  }
  ModInt &operator--() {
    p = (p - 1 + mod) % mod;
    return *this;
  }

  bool operator==(const ModInt &y) const { return p == *y; }
  bool operator!=(const ModInt &y) const { return p != *y; }

  const i64 &operator*() const { return p; }
  i64 &operator*() { return p; }
};

using mint = ModInt<>;

template <typename T> struct Matrix {
  int h, w;
  vector<T> v;

  Matrix() : h(1), w(1), v(1, 1) {}
  Matrix(int n) { *this = makeUnit(n); }
  Matrix(int h, int w) : h(h), w(w), v(h * w, 0) {}

  Matrix(vector<vector<T>> v_) : h(v_.size()), w(v_[0].size()), v(h * w) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        v[i * w + j] = v_[i][j];
  }

  static Matrix makeUnit(int n) {
    Matrix mat(n, n);
    for (int i = 0; i < n; ++i)
      mat.at(i, i) = 1;
    return mat;
  }

  T &at(int i, int j) {
    assert(0 <= i && i <= h && 0 <= j && j < w);
    return v[i * w + j];
  };

  Matrix pow(i64 x) {
    assert(h == w);
    auto mat = x & 1 ? *this : makeUnit(h);
    auto u = *this;
    while (u = u * u, x >>= 1)
      if (x & 1)
        mat *= u;
    return mat;
  }

  Matrix &operator+=(const Matrix &mat) {
    assert(h == mat.h && w == mat.w);
    for (int i = 0; i < h * w; ++i)
      v[i] += mat.v[i];
    return *this;
  }

  Matrix &operator-=(const Matrix &mat) {
    assert(h == mat.h && w == mat.w);
    for (int i = 0; i < h * w; ++i)
      v[i] -= mat.v[i];
    return *this;
  }

  Matrix &operator%=(const T mod) {
    for (int i = 0; i < h * w; ++i)
      v[i] %= mod;
    return *this;
  }

  Matrix operator*(const Matrix &mat) {
    assert(w == mat.h);
    Matrix ret(h, mat.w);
    for (int i = 0; i < h; ++i)
      for (int k = 0; k < w; ++k)
        for (int j = 0; j < mat.w; ++j)
          ret.v[i * mat.w + j] += v[i * w + k] * mat.v[k * mat.w + j];
    return ret;
  }

  Matrix operator+(const Matrix &mat) { return Matrix(*this) += mat; }
  Matrix operator-(const Matrix &mat) { return Matrix(*this) -= mat; }
  Matrix operator%(const T mod) { return Matrix(*this) %= mod; }
  Matrix &operator*=(const Matrix &mat) { return *this = *this * mat; }
};

void solve(long long k, std::string s) {
  int n = s.size();
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    v[i] = s[i] - 'a';

  auto mpow = [](auto x, i64 y) {
    auto z = x;
    decltype(x) val = y & 1 ? x : decltype(x)(1);
    while (z *= z, y >>= 1)
      if (y & 1)
        val *= z;
    return val;
  };

  vector<mint> tf(n + k + 10, 1);
  for (int i = 0; i < n + k + 9; ++i)
    tf[i + 1] = tf[i] * 25;
  vector<mint> ts(n + k + 10, 1);
  for (int i = 0; i < n + k + 9; ++i)
    ts[i + 1] = ts[i] * 26;

  auto calc_fact = [mpow] {
    constexpr int N = 1e5;
    vector<mint> fact(N + 1, 1);
    vector<mint> inv(N + 1, 1);
    for (int i = 1; i < N; ++i) {
      fact[i + 1] = fact[i] * (i + 1);
      inv[i + 1] = mpow(fact[i + 1], MOD - 2);
    }
    return make_pair(fact, inv);
  };
  vector<mint> fact, inv;
  tie(fact, inv) = calc_fact();

  auto ncr = [&fact, &inv](int n, int r) {
    if (n < 0 || r < 0 || n < r)
      return mint(0);
    return fact[n] * inv[r] * inv[n - r];
  };

  // 1文字マッチしてない状態で見る
  // i: マッチ文字数 j: s[i]の位置
  int i = n - 1;
  // int j = n + k;
  mint ans = 0;
  for (int j = n - 1; j <= n + k - 1; ++j) {
    int p1 = j;
    int p2 = j - i;
    mint val = tf[p2] * ncr(p1, p2);
    ans += val * ts[n + k - 1 - j];
  }
  cout << ans << endl;
}

signed main() {
  long long K;
  scanf("%lld", &K);
  std::string S;
  std::cin >> S;
  solve(K, S);
  return 0;
}