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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;
template <typename T> T fastPow(T a, T b, T mod = 0) {
  if (a == 0 and b == 0) {
    return -1;
  }
  if (a == 0) {
    return 0;
  }
  if (b == 0) {
    return 1;
  }
  long long tmp = fastPow(a, b / 2, mod);
  if (mod != 0) {
    if (b % 2 == 0) {
      return (tmp * tmp) % mod;
    } else {
      return ((tmp * tmp) % mod * (a % mod)) % mod;
    }
  } else {
    if (b % 2 == 0) {
      return (tmp * tmp);
    } else {
      return tmp * tmp * a;
    }
  }
}
template <typename T> T extGCD(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T d = extGCD(b, a % b, y, x); // 再帰的に解く
  y -= a / b * x;
  return d;
}

// 負の数にも対応した mod
template <typename T> T modG(T a, T m) { return (a % m + m) % m; }

// 逆元計算
template <typename T> T modinv(T a, T m) {
  T x, y;
  extGCD(a, m, x, y);
  return modG(x, m);
}

// typedef unsigned long long ll;
template <typename T> T nCr(T n, T r, T mod = 0) {
  T ans = 1;
  if (mod == 0) {
    for (T i = 1; i <= r; ++i) {
      ans = ans * (n - i + 1) / i;
    }
  } else {
    for (T i = 1; i <= r; i++) {
      ans = modG(ans * (n - i + 1), mod) * modinv(i, mod);
      ans = modG(ans, mod);
      // mod(i, mod)を定義しておくこと
      // modinv(i, mod)を定義しておくこと
    }
  }
  return ans;
}

ll calc(ll i) {
  ll rev = 26 * (i + 1) - i;
  return rev;
}
int main() {
  ll k;
  string s;
  cin >> k >> s;
  ll n = s.length();
  vector<ll> combinations(k + n, 0);

  for (ll x = 0; x <= k; x++) {
    if (x == 0) {
      combinations[k - x + n - 1] = nCr(k - x + n - 1, n - 1, mop);
    } else {
      combinations[k - x + n - 1] =
          combinations[k - x + n] * modinv(k - x + n, mop);
      combinations[k - x + n - 1] = modG(combinations[k - x + n - 1], mop);
      combinations[k - x + n - 1] *= (k - x + n) - (n - 1);
      combinations[k - x + n - 1] = modG(combinations[k - x + n - 1], mop);
    }
    // cout << k - x + n - 1 << "C" << n - 1 << " = " << combinations[k - x + n
    // - 1] << endl;
  }

  ll ans = 0;
  for (ll x = 0; x <= k; x++) {
    ll tmp = modG(fastPow(25LL, k - x, mop) * fastPow(26LL, x, mop), mop);
    tmp *= combinations[k - x + n - 1];
    tmp = modG(tmp, mop);
    ans += tmp;
    ans = modG(ans, mop);
    // cout << ans << endl;
  }
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <new>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mop = 1000000007;
constexpr ll mop2 = 998244353;

using namespace std;
template <typename T> T fastPow(T a, T b, T mod = 0) {
  if (a == 0 and b == 0) {
    return -1;
  }
  if (a == 0) {
    return 0;
  }
  if (b == 0) {
    return 1;
  }
  long long tmp = fastPow(a, b / 2, mod);
  if (mod != 0) {
    if (b % 2 == 0) {
      return (tmp * tmp) % mod;
    } else {
      return ((tmp * tmp) % mod * (a % mod)) % mod;
    }
  } else {
    if (b % 2 == 0) {
      return (tmp * tmp);
    } else {
      return tmp * tmp * a;
    }
  }
}
template <typename T> T extGCD(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T d = extGCD(b, a % b, y, x); // 再帰的に解く
  y -= a / b * x;
  return d;
}

// 負の数にも対応した mod
template <typename T> T mod(T a, T m) { return (a % m + m) % m; }

// 逆元計算
template <typename T> T modinv(T a, T m) {
  T x, y;
  extGCD(a, m, x, y);
  return mod(x, m);
}

ll calc(ll i) {
  ll rev = 26 * (i + 1) - i;
  return rev;
}

int main() {
  ll k;
  string s;
  cin >> k >> s;
  ll len = s.length() + k;
  vector<vector<ll>> dp(len + 1);
  for (ll i = 0; i < len + 1; i++) {
    dp[i].resize(s.length() + 1);
  }
  dp[0][0] = 1;
  for (ll i = 1; i < len + 1; i++) {
    dp[i][0] = dp[i - 1][0] * 25;
    dp[i][0] = mod(dp[i][0], mop);
  }
  for (ll j = 1; j < s.length() + 1; j++) {
    for (ll i = 1; i < len + 1; i++) {
      dp[i][j] = dp[i - 1][j - 1];
      dp[i][j] = mod(dp[i][j], mop);
    }
  }
  cout << dp[len][s.length()] << endl;
}