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

#include <bits/stdc++.h>
#define rep(i, a, b) for (auto i = (a); i < (b); i++)
#define per(i, a, b) for (auto i = (a); i >= (b); i--)
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define int_inf 0x3f3f3f3f
#define int_max 2147483647
#define ll_max 9223372036854775807
#define ull_max 18446744073709551615
#define ll_inf 0x7f7f7f7f7f7f7f7f
#define lowbit(x) (x) & (-x)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eps 1e-8
#define all(v) (v.begin(), v.end())
#define db double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
const double inf = 1e100;
template <class T> inline T read() {
  T x = 0, f = 1;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  };
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + s - 48;
    s = getchar();
  };
  return x * f;
}
template <typename T> inline void wri(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  };
  if (x / 10)
    wri(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char s) {
  wri(x);
  putchar(s);
}
template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); }
// 关闭流同步就不能手写读入优化啦
const int N = 1e7 + 10;
ll fac[10001100], inv[10100100];
char s[10001100];
int k;
ll quick_pow(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ret;
}
void init() {
  fac[0] = fac[1] = 1;
  for (int i = 2; i < N; i++) {
    fac[i] = (i * fac[i - 1]) % mod;
  }
  inv[N - 1] = quick_pow(fac[N - 1], mod - 2);
  per(i, N - 2, 0) inv[i] = (inv[i + 1] * (i + 1)) % mod;
}
ll C(ll a, ll b) {
  if (b > a)
    return 0ll;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  init();
  cin >> k >> s;
  ll res = 0;
  int len = strlen(s);
  for (int i = len; i <= k + len; i++) {
    res = (res + C(i - 1, len - 1) * quick_pow(25ll, i - len) % mod *
                     quick_pow(26ll, k + len - i) % mod) %
          mod;
  }
  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
ll fac[N], inv[N];
char s[N];
int k;

ll qp(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}

void init() {
  fac[0] = fac[1] = 1;
  for (int i = 2; i < N; i++)
    fac[i] = fac[i - 1] * i % mod;
  inv[N - 1] = qp(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod;
}

ll C(ll a, ll b) {
  if (b > a)
    return 0ll;
  return fac[a] * inv[b] % mod * inv[a - b] % mod;
}

ll add(ll a, ll b) { return (a + b) % mod; }

int main() {
  init();
  scanf("%d", &k);
  scanf("%s", s);

  ll res = 0;
  int len = strlen(s);

  for (int i = len; i <= k + len;
       i++) // 枚举前i个字符为S的所在地，即第i个位置是字符s[len-1]的所在地
    res = add(res, C(i - 1, len - 1) * qp(25ll, i - len) % mod *
                       qp(26ll, k + len - i) % mod);
  printf("%lld\\n", res);

  return 0;
}