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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 2000005;
ll fac[nmax], finv[nmax], inv[nmax];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int 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(int n, int k) {
  if (n < k || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

// 繰り返し二乗法 n*nが64bitにおさまる必要
ll pow_mod(ll n, ll k, ll m) {
  ll res = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1) {
      res = (res * n) % m;
    }
    n = (n * n) % m;
  }
  return res;
}

// 逆元
ll mod_inverse(ll a, ll m) { return pow_mod(a, m - 2, m); }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.size();
  COMinit();
  ll ans = 0;
  REP(i, k + 1) {
    (ans += com(n - 1 + k - i, n - 1) * pow_mod(25, k - i, mod) % mod *
            pow_mod(26, i, mod) % mod) %= mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 1000005;
ll fac[nmax], finv[nmax], inv[nmax];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int 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(int n, int k) {
  if (n < k || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

// 繰り返し二乗法 n*nが64bitにおさまる必要
ll pow_mod(ll n, ll k, ll m) {
  ll res = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1) {
      res = (res * n) % m;
    }
    n = (n * n) % m;
  }
  return res;
}

// 逆元
ll mod_inverse(ll a, ll m) { return pow_mod(a, m - 2, m); }
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.size();
  COMinit();
  ll ans = 0;
  REP(i, k + 1) {
    (ans += com(n - 1 + k - i, n - 1) * pow_mod(25, k - i, mod) % mod *
            pow_mod(26, i, mod) % mod) %= mod;
  }
  cout << ans << endl;
}