Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t n) const {
    if (n == 0)
      return 1;
    mint t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

vector<mint> fac;
vector<mint> fac_inv;
vector<mint> inv;
void init_comb(int mx) {
  fac = vector<mint>(mx + 1), fac_inv = vector<mint>(mx + 1),
  inv = vector<mint>(mx + 1);
  fac.at(0) = fac.at(1) = 1;
  fac_inv.at(0) = fac_inv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i <= mx; i++) {
    fac.at(i) = fac.at(i - 1) * i;
    inv.at(i) = -inv.at(MOD % i) * (MOD / i);
    fac_inv.at(i) = fac_inv.at(i - 1) * inv.at(i);
  }
}
mint comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * fac_inv.at(k) * fac_inv.at(n - k);
}

void solve(int K, string S) {
  init_comb((int)1e6 + 1);
  int len = K + S.size();

  mint res = 0;
  for (int r = 0; r <= K; r++) {
    res +=
        comb(len - r - 1, S.size() - 1) * mint(25).pow(K - r) * mint(26).pow(r);
  }
  cout << res << endl;
}

int main() {
  int K;
  cin >> K;
  string S;
  cin >> S;
  solve(K, S);
  return 0;
}


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

const int MOD = 1000000007;

struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t n) const {
    if (n == 0)
      return 1;
    mint t = pow(n >> 1);
    t *= t;
    if (n & 1)
      t *= *this;
    return t;
  }
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

vector<mint> fac;
vector<mint> fac_inv;
vector<mint> inv;
void init_comb(int mx) {
  fac = vector<mint>(mx + 1), fac_inv = vector<mint>(mx + 1),
  inv = vector<mint>(mx + 1);
  fac.at(0) = fac.at(1) = 1;
  fac_inv.at(0) = fac_inv.at(1) = 1;
  inv.at(1) = 1;
  for (int i = 2; i <= mx; i++) {
    fac.at(i) = fac.at(i - 1) * i;
    inv.at(i) = -inv.at(MOD % i) * (MOD / i);
    fac_inv.at(i) = fac_inv.at(i - 1) * inv.at(i);
  }
}
mint comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac.at(n) * fac_inv.at(k) * fac_inv.at(n - k);
}

void solve(int K, string S) {
  init_comb((int)1e6 * 2 + 1);
  int len = K + S.size();

  mint res = 0;
  for (int r = 0; r <= K; r++) {
    res +=
        comb(len - r - 1, S.size() - 1) * mint(25).pow(K - r) * mint(26).pow(r);
  }
  cout << res << endl;
}

int main() {
  int K;
  cin >> K;
  string S;
  cin >> S;
  solve(K, S);
  return 0;
}