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

#include <bits/stdc++.h>
using namespace std;
// mod逆元を使ったnCkの計算(速)
const int64_t MAX = 3000010;
const int64_t MOD = 1000000007;

int64_t fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

// 二項係数計算
int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 繰り返し二乗法
int64_t powmod(int64_t x, int64_t n) {
  int64_t ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x;
      ans %= MOD;
    }
    x = x * x; // 一周する度にx, x^2, x^4, x^8となる
    x %= MOD;
    n >>= 1; // 桁をずらす n = n >> 1
  }
  return ans;
}

int main() {
  int64_t K;
  cin >> K;
  string S;
  cin >> S;
  int64_t N = S.size();

  /*dp[0][0] = 25;
  dp[0][1] = 1;
  for(int i=0; i<N+K; i++){
    for(int j=0; j<N+1; j++){
      if(j == 0){
        dp[i+1][j] = dp[i][j]*25;
      }
      else if(j != N){
        dp[i+1][j] = dp[i][j-1] + dp[i][j]*25;
      }
      else{
        dp[i+1][j] = dp[i][j]*26 + dp[i][j-1];
      }
      dp[i+1][j] %= MOD;
    }
  }
  cout << dp[N+K-1][N] << endl;*/

  COMinit();
  int64_t ans = 0;
  for (int i = 0; i <= K; i++) {
    int64_t plus = COM(N + i - 1, i);
    plus *= powmod(25, i);
    plus %= MOD;
    plus *= powmod(26, K - i);
    plus %= MOD;
    ans += plus;
    ans %= MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// mod逆元を使ったnCkの計算(速)
const int64_t MAX = 1000010;
const int64_t MOD = 1000000007;

int64_t fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; 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;
  }
}

// 二項係数計算
int64_t COM(int64_t n, int64_t k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// 繰り返し二乗法
int64_t powmod(int64_t x, int64_t n) {
  int64_t ans = 1;
  while (n > 0) {
    if ((n & 1) == 1) {
      ans = ans * x;
      ans %= MOD;
    }
    x = x * x; // 一周する度にx, x^2, x^4, x^8となる
    x %= MOD;
    n >>= 1; // 桁をずらす n = n >> 1
  }
  return ans;
}

int main() {
  int64_t K;
  cin >> K;
  string S;
  cin >> S;
  int64_t N = S.size();

  /*dp[0][0] = 25;
  dp[0][1] = 1;
  for(int i=0; i<N+K; i++){
    for(int j=0; j<N+1; j++){
      if(j == 0){
        dp[i+1][j] = dp[i][j]*25;
      }
      else if(j != N){
        dp[i+1][j] = dp[i][j-1] + dp[i][j]*25;
      }
      else{
        dp[i+1][j] = dp[i][j]*26 + dp[i][j-1];
      }
      dp[i+1][j] %= MOD;
    }
  }
  cout << dp[N+K-1][N] << endl;*/

  COMinit();
  int64_t ans = 0;
  for (int i = 0; i <= K; i++) {
    int64_t plus = COM(N + i - 1, i);
    plus *= powmod(25, i);
    plus %= MOD;
    plus *= powmod(26, K - i);
    plus %= MOD;
    ans += plus;
    ans %= MOD;
  }
  cout << ans << endl;
}