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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 2e6;
const char newl = '\\n';

ll modpow(ll m, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modpow(m, n / 2) * modpow(m, n / 2) % MOD;
  else
    return m * modpow(m, n - 1) % MOD;
}

vl get_inv(ll p, ll n) {
  vl inv(n + 1);
  inv[1] = 1;
  for (ll i = 2; i <= n; i++)
    inv[i] = p - inv[p % i] * (p / i) % p;
  return inv;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll k;
  string s;
  cin >> k >> s;

  ll S = s.size();

  vl inv = get_inv(MOD, MAX);

  vl factorial(MAX + 1);
  factorial[0] = 1;
  rep(i, MAX) factorial[i + 1] = (factorial[i] * (i + 1)) % MOD;
  vl inv_fac(MAX + 1);
  inv_fac[0] = 1;
  rep(i, MAX) inv_fac[i + 1] = (inv_fac[i] * inv[i + 1]) % MOD;

  ll ans = 0;
  rep(K, k + 1) {
    ll num = modpow(26, k - K);
    (num *= modpow(25, K)) %= MOD;
    (num *= factorial[S + K - 1]) %= MOD;
    (num *= inv_fac[S - 1]) %= MOD;
    (num *= inv_fac[K]) %= MOD;
    (ans += num) %= MOD;
  }
  cout << ans << newl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll /*, vl, greater<ll>*/> pql; // 降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define rep2(i, n) for (ll i = 1; i <= ll(n); i++)
// #define rep(i, k, n) for(ll i = k-1; i < ll(n); i++)
// #define rep2(i, k, n) for(ll i = k; i <= ll(n); i++)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
bool chmax(ll &a, ll b) {
  if (b > a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\\n';

ll modpow(ll m, ll n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modpow(m, n / 2) * modpow(m, n / 2) % MOD;
  else
    return m * modpow(m, n - 1) % MOD;
}

vl get_inv(ll p, ll n) {
  vl inv(n + 1);
  inv[1] = 1;
  for (ll i = 2; i <= n; i++)
    inv[i] = p - inv[p % i] * (p / i) % p;
  return inv;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  ll k;
  string s;
  cin >> k >> s;

  ll S = s.size();

  vl inv = get_inv(MOD, 1e6);

  ll ans = 0;
  rep(K, k + 1) {
    ll num = modpow(26, k - K);
    (num *= modpow(25, K)) %= MOD;
    rep2(i, S + K - 1)(num *= i) %= MOD;
    rep2(i, S - 1)(num *= inv[i]) %= MOD;
    rep2(i, K)(num *= inv[i]) %= MOD;
    (ans += num) %= MOD;
  }
  cout << ans << newl;
  return 0;
}