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

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//             tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int N = 2e6 + 2;

int fac[N], inv[N];

int mi(int x, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = (1LL * res * x) % mod;
    x = (1LL * x * x) % mod;
    n >>= 1;
  }
  return res;
}

void fact() {
  fac[0] = 1;
  rep(i, 1, N) { fac[i] = (1LL * fac[i - 1] * i) % mod; }

  inv[N - 1] = mi(fac[N - 1], mod - 2);
  for (int i = N - 1; i > 0; i--) {
    inv[i - 1] = (1LL * inv[i] * i) % mod;
  }
}

int nCr(int n, int k) {
  if (k > n)
    return 0;

  int ans = (1LL * fac[n] * inv[k]) % mod;
  ans = (1LL * ans * inv[n - k]) % mod;
  return ans;
}

void solve() {
  fact();

  int k;
  string s;

  cin >> k >> s;

  int m = (int)s.size();

  int ans = 0;

  for (int x = 0; x <= k; x++) {
    // 26^x * (k-x+m-1)Cm-1 * 25^(k-x)

    int here = mi(26, x);
    here = (1LL * here * nCr(k - x + m - 1, m - 1)) % mod;
    here = (1LL * here * mi(25, k - x)) % mod;
    ans = (1LL * ans + here) % mod;
  }

  prin(ans);
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

/* Simplicity and Goodness */

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

// using namespace __gnu_pbds;
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
//             tree_order_statistics_node_update> indexed_set;

void my_dbg() { cout << endl; }
template <typename Arg, typename... Args> void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)

#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\\n", n)
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;

const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int N = 1e6 + 2;

int fac[N], inv[N];

int mi(int x, int n) {
  int res = 1;
  while (n) {
    if (n & 1)
      res = (1LL * res * x) % mod;
    x = (1LL * x * x) % mod;
    n >>= 1;
  }
  return res;
}

void fact() {
  fac[0] = 1;
  rep(i, 1, N) { fac[i] = (1LL * fac[i - 1] * i) % mod; }

  inv[N - 1] = mi(fac[N - 1], mod - 2);
  for (int i = N - 1; i > 0; i--) {
    inv[i - 1] = (1LL * inv[i] * i) % mod;
  }
}

int nCr(int n, int k) {
  if (k > n)
    return 0;

  int ans = (1LL * fac[n] * inv[k]) % mod;
  ans = (1LL * ans * inv[n - k]) % mod;
  return ans;
}

void solve() {
  fact();

  int k;
  string s;

  cin >> k >> s;

  int m = (int)s.size();

  int ans = 0;

  for (int x = 0; x <= k; x++) {
    // 26^x * (k-x+m-1)Cm-1 * 25^(k-x)

    int here = mi(26, x);
    here = (1LL * here * nCr(k - x + m - 1, m - 1)) % mod;
    here = (1LL * here * mi(25, k - x)) % mod;
    ans = (1LL * ans + here) % mod;
  }

  prin(ans);
}

int main() {
  int t = 1;
  // scn(t);

  while (t--) {
    solve();
  }
  return 0;
}