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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// ll comb_mod(ll a, ll b, ll mod){
//     ll num = 1;
//     ll div = 1;
//     for(int i = 0; i < b; i++){
//         num = (num * (a-i)) % mod;
//         div = (div * (b-i)) % mod;
//     }
//
//     ll ret = (num * mod_pow(div, mod-2, mod)) % mod;
//     return ret;
// }

const int MAX_N = 2000000;

ll fact[MAX_N + 100];
ll frev[MAX_N + 100];

ll rev(ll a, ll p) {
  if (p == 0)
    return 1;
  ll tmp = rev(a, p / 2);
  tmp = tmp * tmp % MOD;
  if (p % 2 == 1)
    tmp = tmp * a % MOD;
  return tmp;
}

void calc_fact() {
  fact[0] = frev[0] = 1;
  for (int i = 1; i <= MAX_N; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    frev[i] = rev(fact[i], MOD - 2);
  }
}

ll comb(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (n == 0 || k == 0)
    return 1;
  return fact[n] * frev[k] % MOD * frev[n - k] % MOD;
}

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

  // in main function
  fact[0] = frev[0] = 1;
  for (int i = 1; i <= MAX_N; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    frev[i] = rev(fact[i], MOD - 2);
  }

  ll size = S.size();

  ll ans = 0;
  for (ll m = 0; m <= K; m++) {
    ll right = mod_pow(26, m, MOD);
    ll left = mod_pow(25, K - m, MOD);
    // ll com = comb_mod(K-m+size-1, size-1, MOD);
    ll com = comb(K - m + size - 1, size - 1);

    ll add = right * left;
    add %= MOD;
    add *= com;
    add %= MOD;

    ans += add;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPi(i, a, b) for (int i = int(a); i < int(b); i++)
#define MEMS(a, b) memset(a, b, sizeof(a))
#define mp make_pair
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

// ll comb_mod(ll a, ll b, ll mod){
//     ll num = 1;
//     ll div = 1;
//     for(int i = 0; i < b; i++){
//         num = (num * (a-i)) % mod;
//         div = (div * (b-i)) % mod;
//     }
//
//     ll ret = (num * mod_pow(div, mod-2, mod)) % mod;
//     return ret;
// }

const int MAX_N = 200000;

ll fact[MAX_N + 100];
ll frev[MAX_N + 100];

ll rev(ll a, ll p) {
  if (p == 0)
    return 1;
  ll tmp = rev(a, p / 2);
  tmp = tmp * tmp % MOD;
  if (p % 2 == 1)
    tmp = tmp * a % MOD;
  return tmp;
}

void calc_fact() {
  fact[0] = frev[0] = 1;
  for (int i = 1; i <= MAX_N; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    frev[i] = rev(fact[i], MOD - 2);
  }
}

ll comb(int n, int k) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  if (n == 0 || k == 0)
    return 1;
  return fact[n] * frev[k] % MOD * frev[n - k] % MOD;
}

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

  // in main function
  fact[0] = frev[0] = 1;
  for (int i = 1; i <= MAX_N; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    frev[i] = rev(fact[i], MOD - 2);
  }

  ll size = S.size();

  ll ans = 0;
  for (ll m = 0; m <= K; m++) {
    ll right = mod_pow(26, m, MOD);
    ll left = mod_pow(25, K - m, MOD);
    // ll com = comb_mod(K-m+size-1, size-1, MOD);
    ll com = comb(K - m + size - 1, size - 1);

    ll add = right * left;
    add %= MOD;
    add *= com;
    add %= MOD;

    ans += add;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}