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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

ll moddividing(ll x, ll y) {
  vector<ll> vec(30);
  rep(i, 30) {
    vec[i] = y;
    y = y * y % 1000000007;
  }
  ll c = x;
  c = c * vec[0] % 1000000007;
  c = c * vec[2] % 1000000007;
  c = c * vec[9] % 1000000007;
  c = c * vec[11] % 1000000007;
  c = c * vec[14] % 1000000007;
  c = c * vec[15] % 1000000007;
  c = c * vec[17] % 1000000007;
  c = c * vec[19] % 1000000007;
  c = c * vec[20] % 1000000007;
  c = c * vec[23] % 1000000007;
  c = c * vec[24] % 1000000007;
  c = c * vec[25] % 1000000007;
  c = c * vec[27] % 1000000007;
  c = c * vec[28] % 1000000007;
  c = c * vec[29] % 1000000007;
  return c;
}

int main() {
  ll K;
  cin >> K;
  string S;
  cin >> S;
  ll len = S.size();
  vector<ll> exp_25(2000001);
  ll num = 1;
  for (ll i = 0; i < 2000001; i++) {
    exp_25[i] = num;
    num = (num * 25) % 1000000007;
  }
  vector<ll> exp_26(2000001);
  num = 1;
  for (ll i = 0; i < 2000001; i++) {
    exp_26[i] = num;
    num = (num * 26) % 1000000007;
  }
  vector<ll> fact(2000001);
  num = 1;
  for (ll i = 0; i < 2000001; i++) {
    fact[i] = num;
    num = (num * (i + 1)) % 1000000007;
  }
  ll ans = 0;
  for (ll i = len; i <= len + K; i++) {
    ll comb =
        moddividing(fact[i - 1], (fact[len - 1] * fact[i - len]) % 1000000007);
    comb = (comb * exp_25[i - len]) % 1000000007;
    comb = (comb * exp_26[len + K - i]) % 1000000007;
    ans = (ans + comb) % 1000000007;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #define int long long
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,
                         tree_order_statistics_node_update>;
// find_by_order(k):returns iterator to kth element starting from 0
// order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> T gcd(T a, T b) { return (b ? __gcd(a, b) : a); }
template <typename T> T lcm(T a, T b) { return (a * (b / gcd(a, b))); }
template <typename T> void remdup(vector<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <typename T> void re(vector<T> &v) { trav(i, v) cin >> i; }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
#ifdef np
#include "C:\\Users\\navodit\\Desktop\\mycodes\\pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

// ref : https://codeforces.com/blog/entry/63903
// Verification : https://codeforces.com/contest/1248/submission/86484042
//				: CSES Maths section
// comment define int long long

ll modinv(ll a, ll m) {
  assert(m > 0);
  if (m == 1)
    return 0;
  a %= m;
  if (a < 0)
    a += m;
  assert(a != 0);
  if (a == 1)
    return 1;
  return m - modinv(m, a) * m / a;
}

template <int MOD_> struct modnum {
private:
  int v;

public:
  static const int MODM = MOD_;

  modnum() : v(0) {}
  modnum(ll v_) : v(int(v_ % MODM)) {
    if (v < 0)
      v += MODM;
  }
  explicit operator int() const { return v; }
  friend bool operator==(const modnum &a, const modnum &b) {
    return a.v == b.v;
  }
  friend bool operator!=(const modnum &a, const modnum &b) {
    return a.v != b.v;
  }

  modnum operator~() const {
    modnum res;
    res.v = modinv(v, MODM);
    return res;
  }

  modnum &operator+=(const modnum &o) {
    v += o.v;
    if (v >= MODM)
      v -= MODM;
    return *this;
  }
  modnum &operator-=(const modnum &o) {
    v -= o.v;
    if (v < 0)
      v += MODM;
    return *this;
  }
  modnum &operator*=(const modnum &o) {
    v = int(ll(v) * ll(o.v) % MODM);
    return *this;
  }
  modnum &operator/=(const modnum &o) { return *this *= (~o); }

  friend modnum operator+(const modnum &a, const modnum &b) {
    return modnum(a) += b;
  }
  friend modnum operator-(const modnum &a, const modnum &b) {
    return modnum(a) -= b;
  }
  friend modnum operator*(const modnum &a, const modnum &b) {
    return modnum(a) *= b;
  }
  friend modnum operator/(const modnum &a, const modnum &b) {
    return modnum(a) /= b;
  }
};
// #define MOD 998244353
using mi = modnum<MOD>;
mi powr(mi a, ll p) {
  mi ans = 1;
  assert(p >= 0);
  for (; p; p /= 2, a *= a)
    if (p & 1)
      ans *= a;
  return ans;
}
mi inv(const mi &a) { return powr(a, MOD - 2); }

vector<mi> fact;
vector<mi> ifact;

void init() {
  int N = 1100000; // set
  fact = {1};
  for (int i = 1; i < N; i++)
    fact.push_back(i * fact[i - 1]);
  ifact.resize(N);
  ifact.back() = 1 / fact.back();
  for (int i = N - 1; i > 0; i--)
    ifact[i - 1] = i * ifact[i];
}

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

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll tot = n + sz(s);
  mi ans = 0;
  rep(i, sz(s), tot) { ans += (powr(25, tot - i) * ncr(tot, i)); }
  cout << (int)ans + 1 << endl;
}

signed main() {
  unsyncIO();
  init();
  int tt = 1;
  // cin >> tt;
  rep(i, 0, tt) solve();

#ifdef np
  cout << endl
       << endl
       << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}