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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
ll const INF = 1LL << 60;
ll const MAX_N = 510000;
ll const MOD = 1000000007;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }

  mint operator+(const mint a) const { return mint(*this) += a; }

  mint operator-(const mint a) const { return mint(*this) -= a; }

  mint operator*(const mint a) const { return mint(*this) *= a; }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};

istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct combination {
  vector<mint> fact, ifact;

  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < MOD);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }

  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

int main() {
  ll K;
  string S;
  cin >> K >> S;

  ll len = K + S.length();
  combination comb(len);
  mint ans = 0;

  for (ll N = S.length() - 1; N < len; N++) {
    ll m = len - N - 1;
    mint now = comb(N, S.length() - 1);
    now *= mint(25).pow(K - m);
    now *= mint(26).pow(m);
    ans += now;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
ll const INF = 1LL << 60;
ll const MAX_N = 510000;
ll const MOD = 1000000007;

vector<ll> fac;  // a! に関するテーブル 階乗計算 O(n)
vector<ll> finv; // (a!)^-1 に関するテーブル invの累積積
vector<ll> inv;  // mod pにおける1, 2, ..., nの逆元 O(n)

// iの逆元は、p % iの逆元を利用してO(1)で求まる
// ref: https://algo-logic.info/combination/#toc_id_1_2

// 二項係数 nCr (mod p) の計算
// k <= n <= 1e7でpが素数のとき

// テーブルを作る前処理
// 計算量 O(n)
void comb_init() {
  fac.resize(MAX_N);
  finv.resize(MAX_N);
  inv.resize(MAX_N);

  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < MAX_N; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数の計算
// 計算量 O(1)
ll comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 二分累乗法 O(logn)
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll K;
  string S;
  cin >> K >> S;

  comb_init();

  ll len = K + S.length();
  ll ans = 0;

  for (ll N = S.length() - 1; N < len; N++) {
    ll m = len - N - 1;
    // cout << "len-m-1:" << len - m - 1 << ", N:" << N << endl;
    // cout << "m:" << m << ", comb:" << comb(N, S.length() - 1)
    //      << ", pow25: " << modpow(25, K - m, MOD)
    //      << ", pow26:" << modpow(26, m, MOD) << endl;

    ans += comb(N, S.length() - 1) *
           (modpow(25, K - m, MOD) * modpow(26, m, MOD) % MOD) % MOD;
    ans = ans % MOD;
  }
  cout << ans << endl;

  return 0;
}