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

/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#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;
#define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define ar array
// #define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAX = 2000005;
int fac[MAX], inv[MAX], finv[MAX];

void pre() {
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }
  fac[0] = finv[0] = 1;
  for (int i = 1; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
int ncr(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  return fac[n] * finv[r] % mod * finv[n - r] % mod;
}

int modpow(int x, int n, int m) {
  if (n == 0)
    return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1)
    u = (u * x) % m;
  return u;
}

string s;
int k;

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  pre();
  cin >> k;
  cin >> s;
  int n = s.size();
  int ans = modpow(26, n + k, mod);
  for (int i = 0; i < n; i++) {
    ans = (ans - ncr(n + k, i) * modpow(25, n + k - i, mod) % mod + mod) % mod;
  }
  cout << ans << endl;

  return 0;
}
///....


Error: 0

Fixed submission:

/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#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;
#define int long long
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define ar array
// #define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
template <class S, class T>
ostream &operator<<(ostream &o, const pair<S, T> &p) {
  return o << "(" << p.fs << "," << p.sc << ")";
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) {
  o << "{";
  for (const T &v : vc)
    o << v << ",";
  o << "}";
  return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAX = 1000005;
int fac[MAX], inv[MAX], finv[MAX];

void pre() {
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }
  fac[0] = finv[0] = 1;
  for (int i = 1; i < MAX; ++i) {
    fac[i] = fac[i - 1] * i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
int ncr(int n, int r) {
  if (r < 0 || r > n)
    return 0;
  return fac[n] * finv[r] % mod * finv[n - r] % mod;
}

int modpow(int x, int n, int m) {
  if (n == 0)
    return 1 % m;
  long long u = modpow(x, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1)
    u = (u * x) % m;
  return u;
}

string s;
int k;

signed main() {
  IOS;
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  pre();
  cin >> k;
  cin >> s;
  int n = s.size();
  int ans = modpow(26, n + k, mod);
  for (int i = 0; i < n; i++) {
    ans = (ans - ncr(n + k, i) * modpow(25, n + k - i, mod) % mod + mod) % mod;
  }
  cout << ans << endl;

  return 0;
}
///....