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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define gll greater<ll>()

const ll nmax = 2000005;

ll fac[nmax], finv[nmax], inv[nmax];

void comint() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < nmax; 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;
  }
}

ll com(ll n, ll k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

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

void solve() {
  ll k;
  string s;
  cin >> k >> s;
  ll n = s.size();
  comint();
  ll ans = 0;
  repr(i, n + k, n - 1) {
    ll r = n + k - 1 - i;
    ll tot = mod_pow(26, r) * mod_pow(25, k - r);
    tot %= mod;
    tot *= com(n + k - r - 1, n - 1);
    tot %= mod;
    ans += tot;
    ans %= mod;
  }

  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod = 1e9 + 7;
const ll inf = 1e16;

#define rep(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m - 1; i >= n; i--)
#define fi first
#define se second
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(), x.end()
#define gll greater<ll>()

const ll nmax = 1000000;

ll fac[nmax], finv[nmax], inv[nmax];

void comint() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < nmax; 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;
  }
}

ll com(ll n, ll k) {
  if (n < k || n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

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

void solve() {
  ll k;
  string s;
  cin >> k >> s;
  ll n = s.size();
  comint();
  ll ans = 0;
  repr(i, n + k, n - 1) {
    ll r = n + k - 1 - i;
    ll tot = mod_pow(26, r) * mod_pow(25, k - r);
    tot %= mod;
    tot *= com(n + k - r - 1, n - 1);
    tot %= mod;
    ans += tot;
    ans %= mod;
  }

  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}