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

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

int modpow(int a, int p) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    int halfP = p / 2;
    int half = modpow(a, halfP);
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1) % mod;
  }
}

vector<int> fact, fact_inv, inv;

/*  init_nCk :二項係数のための前処理
        計算量:O(n)
*/
void init_nCk(int SIZE) {
  fact.resize(SIZE + 5);
  fact_inv.resize(SIZE + 5);
  inv.resize(SIZE + 5);
  fact[0] = fact[1] = 1;
  fact_inv[0] = fact_inv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE + 5; i++) {
    fact[i] = fact[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % mod;
  }
}

/*  nCk :MODでの二項係数を求める(前処理 int_nCk が必要)
        計算量:O(1)
*/
long long nCk(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % mod) % mod;
}
signed main(void) {
  string s;
  int k;
  cin >> k >> s;
  int n = s.size();
  int ans = 0;
  init_nCk(n + k);
  rep(i, k + 1) {
    int tmp = modpow(25, k - i);
    tmp *= modpow(26, i);
    tmp %= mod;
    tmp *= nCk((k - i + n - 1), (n - 1));
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  print(ans);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

int modpow(int a, int p) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    int halfP = p / 2;
    int half = modpow(a, halfP);
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1) % mod;
  }
}

int calcComb(int a, int b) {
  if (b > a - b)
    return calcComb(a, a - b);
  int ansMul = 1;
  int ansDiv = 1;
  rep(i, b) {
    ansMul *= (a - i);
    ansDiv *= (i + 1);
    ansMul %= mod;
    ansDiv %= mod;
  }
  int ans = ansMul * modpow(ansDiv, mod - 2) % mod;
  return ans;
}

signed main(void) {
  string s;
  int k;
  cin >> k >> s;
  int n = s.size();
  int ans = 0;
  rep(i, k + 1) {
    int tmp = modpow(25, k - i);
    tmp *= modpow(26, i);
    tmp %= mod;
    tmp *= calcComb((k - i + n - 1), (n - 1));
    tmp %= mod;
    ans += tmp;
    ans %= mod;
  }
  print(ans);
}