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

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline void fastio() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
}

#define whats(x) cerr << #x << " = " << (x) << "\\n";

using LL = long long;
using LD = long double;

const LL INF = LLONG_MAX;
const LL MOD = static_cast<LL>(1e9 + 7);
const LL N = 2e6 + 1;

LL fac[N] = {0};

LL modinv_fac[N] = {0};

LL binpow(LL x, LL p, LL mod) {
  LL res = 1;
  x %= mod;
  while (p) {
    if (p & 1) {
      res *= x;
      res %= mod;
    }
    x *= x;
    x %= mod;
    p >>= 1;
  }
  return res;
}

LL binom(LL n, LL r, LL mod) {
  if (r < 0 || r > n) {
    return 0;
  } else {
    LL term = fac[n];
    // LL den = binpow(fac[n-r], mod-2, mod)*binpow(fac[r], mod-2, mod) % mod;
    LL den = modinv_fac[n - r] * modinv_fac[r] % mod;
    return (term * den) % mod;
  }
}

int main() {
  fastio();

  // compute fac array
  fac[0] = 1;
  for (LL i = 1; i < N; ++i) {
    fac[i] = i * fac[i - 1] % MOD;
  }

  // precompute modinv_fac and use this in binpow
  modinv_fac[N - 1] = binpow(fac[N - 1], MOD - 2, MOD);

  for (LL i = N - 2; i >= 0; --i) {
    modinv_fac[i] = (i + 1) * modinv_fac[i + 1] % MOD;
  }

  LL k;
  cin >> k;
  string s;
  cin >> s;

  LL res = 0;

  LL S = s.size();
  for (LL x = 0; x <= k; ++x) {
    LL term = (binpow(25, k - x, MOD) * binpow(26, x, MOD) % MOD);
    term = term * binom(S + k - 1 - x, S - 1, MOD) % MOD;
    res = (res + term) % MOD;
  }

  cout << res << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline void fastio() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
}

#define whats(x) cerr << #x << " = " << (x) << "\\n";

using LL = long long;
using LD = long double;

const LL INF = LLONG_MAX;
const LL MOD = static_cast<LL>(1e9 + 7);
const LL N = 1e6 + 1;

LL fac[N] = {0};

LL modinv_fac[N] = {0};

LL binpow(LL x, LL p, LL mod) {
  LL res = 1;
  x %= mod;
  while (p) {
    if (p & 1) {
      res *= x;
      res %= mod;
    }
    x *= x;
    x %= mod;
    p >>= 1;
  }
  return res;
}

LL binom(LL n, LL r, LL mod) {
  if (r < 0 || r > n) {
    return 0;
  } else {
    LL term = fac[n];
    // LL den = binpow(fac[n-r], mod-2, mod)*binpow(fac[r], mod-2, mod) % mod;
    LL den = modinv_fac[n - r] * modinv_fac[r] % mod;
    return (term * den) % mod;
  }
}

int main() {
  fastio();

  // compute fac array
  fac[0] = 1;
  for (LL i = 1; i < N; ++i) {
    fac[i] = i * fac[i - 1] % MOD;
  }

  // precompute modinv_fac and use this in binpow
  modinv_fac[N - 1] = binpow(fac[N - 1], MOD - 2, MOD);

  for (LL i = N - 2; i >= 0; --i) {
    modinv_fac[i] = (i + 1) * modinv_fac[i + 1] % MOD;
  }

  LL k;
  cin >> k;
  string s;
  cin >> s;

  LL res = 0;

  LL S = s.size();
  for (LL x = 0; x <= k; ++x) {
    LL term = (binpow(25, k - x, MOD) * binpow(26, x, MOD) % MOD);
    term = term * binom(S + k - 1 - x, S - 1, MOD) % MOD;
    res = (res + term) % MOD;
  }

  cout << res << "\\n";
}