Problem p02632 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
// 型シノニム
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
// 定数
constexpr ll INF = 1e16;
constexpr ll INM = 114514;
constexpr ll MOD = 1e9 + 7;
constexpr ld EPS = 1e-12;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
// 便利関数
void IOS() { ios::sync_with_stdio(false), cin.tie(0); }
template <typename T> void dump(T x) { cout << x << endl; }
void dumpf(ld x, ll t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
inline ll mod(ll a, ll mod) {
  if (a > 0)
    return a % mod;
  if (a % mod == 0)
    return 0;
  ll x = -a / mod + 1;
  a += x * mod;
  return a % mod;
}
ll powm(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    ll d = powm(a, b / 2, m);
    return (d * d) % m;
  } else {
    return (powm(a, b - 1, m) * a) % m;
  }
}
// 本体
constexpr ll MAXN = 2e6 + 5;
ll fact[MAXN + 1] = {};
ll ifact[MAXN + 1] = {};
void init_fact(ll n, ll mod) {
  fact[0] = ifact[0] = 1;
  for (int i = 0; i < n; i++) {
    fact[i + 1] = ((i + 1) * fact[i]) % mod;
    ifact[i + 1] = powm(fact[i + 1], mod - 2, mod);
  }
}

ll nci_m(ll n, ll i, ll mod) {
  return fact[n] * ifact[i] % mod * ifact[n - i] % mod;
}

ll pow25[2145141];
ll pow26[2145141];

int main() {
  IOS();
  ll k;
  string s;
  cin >> k >> s;
  ll n = s.size();
  pow25[0] = pow26[0] = 1;
  for (int i = 0; i < k; i++) {
    pow25[i + 1] = pow25[i] * 25 % MOD;
    pow26[i + 1] = pow26[i] * 26 % MOD;
  }
  init_fact(k + n, MOD);
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll tmp =
        (nci_m(n + i - 1, n - 1, MOD) * pow25[i]) % MOD * pow26[k - i] % MOD;
    ans += tmp;
    ans %= MOD;
  }
  dump(ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
// 型シノニム
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
// 定数
constexpr ll INF = 1e16;
constexpr ll INM = 114514;
constexpr ll MOD = 1e9 + 7;
constexpr ld EPS = 1e-12;
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy[4] = {0, 1, 0, -1};
// 便利関数
void IOS() { ios::sync_with_stdio(false), cin.tie(0); }
template <typename T> void dump(T x) { cout << x << endl; }
void dumpf(ld x, ll t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
inline ll mod(ll a, ll mod) {
  if (a > 0)
    return a % mod;
  if (a % mod == 0)
    return 0;
  ll x = -a / mod + 1;
  a += x * mod;
  return a % mod;
}
ll powm(ll a, ll b, ll m) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    ll d = powm(a, b / 2, m);
    return (d * d) % m;
  } else {
    return (powm(a, b - 1, m) * a) % m;
  }
}
// 本体
constexpr ll MAXN = 2e6 + 5;
ll fact[MAXN + 1] = {};
ll ifact[MAXN + 1] = {};
void init_fact(ll n, ll mod) {
  fact[0] = ifact[0] = 1;
  for (int i = 0; i < n; i++) {
    fact[i + 1] = ((i + 1) * fact[i]) % mod;
    ifact[i + 1] = powm(fact[i + 1], mod - 2, mod);
  }
}

ll nci_m(ll n, ll i, ll mod) {
  return fact[n] * ifact[i] % mod * ifact[n - i] % mod;
}

ll pow25[2145141];
ll pow26[2145141];

ll cpow25(int i) {
  if (pow25[i] != -1) {
    return pow25[i];
  } else {
    pow25[i] = powm(25, i, MOD);
    return pow25[i];
  }
}

ll cpow26(int i) {
  if (pow26[i] != -1) {
    return pow26[i];
  } else {
    pow26[i] = powm(26, i, MOD);
    return pow26[i];
  }
}

int main() {
  IOS();
  ll k;
  string s;
  cin >> k >> s;
  ll n = s.size();
  Fill(pow25, -1);
  Fill(pow26, -1);
  init_fact(k + n, MOD);
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll tmp =
        (nci_m(n + i - 1, n - 1, MOD) * cpow25(i)) % MOD * cpow26(k - i) % MOD;
    ans += tmp;
    ans %= MOD;
  }
  dump(ans);
}