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

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;

int modPow(long long a, long long n, long long p) {
  if (n == 0)
    return 1; // 0乗にも対応する場合
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}

long long modMult(long long a, long long b, long long m) {
  long long res = 0;
  long long exp = a % m;
  while (b) {
    if (b & 1) {
      res += exp;
      if (res > m)
        res -= m;
    }
    exp <<= 1;
    if (exp > m)
      exp -= m;
    b >>= 1;
  }
  return res;
}
long long powMod(long long a, long long b, long long m) {
  long long res = 1;
  long long exp = a % m;
  while (b) {
    if (b & 1)
      res = modMult(res, exp, m);
    exp = modMult(exp, exp, m);
    b >>= 1;
  }
  return res;
}
const int MAX = 5 * 1e6;
long long 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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long COM_smallK(ll n, ll k) {
  ll res = 1;
  rep3(i, n - k + 1, n + 1) {
    res *= i;
    res %= MOD;
  }
  return res * finv[k] % MOD;
}
// powMod(a,b,MOD)
// O(log(b))?
// cout << "Case #" << index << " :IMPOSSIBLE";
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll k;
  COMinit();
  string s;
  cin >> k >> s;
  ll n = s.size();
  ll res = 0;
  rep(i, k + 1) {
    ll g = COM(n + k - 1 - i, n - 1);
    ll f = modPow(25, k - i, MOD) % MOD * modPow(26, i, MOD);
    if (f >= MOD)
      f %= MOD;
    ll h = f * g;
    if (h >= MOD)
      h %= MOD;
    res += h;
    if (res >= MOD)
      res %= MOD;
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

/*
    Author:zeke

    pass System Test!
    GET AC!!
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
#define Scin(s)                                                                \\
  ll(s);                                                                       \\
  cin >> (s);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
ll MOD = 1e9 + 7;
ll INF = 1e18;
long long modMult(long long a, long long b, long long m) {
  long long res = 0;
  long long exp = a % m;
  while (b) {
    if (b & 1) {
      res += exp;
      if (res > m)
        res -= m;
    }
    exp <<= 1;
    if (exp > m)
      exp -= m;
    b >>= 1;
  }
  return res;
}
long long powMod(long long a, long long b, long long m) {
  long long res = 1;
  long long exp = a % m;
  while (b) {
    if (b & 1)
      res = modMult(res, exp, m);
    exp = modMult(exp, exp, m);
    b >>= 1;
  }
  return res;
}
const int MAX = 5 * 1e6;
long long 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;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long COM_smallK(ll n, ll k) {
  ll res = 1;
  rep3(i, n - k + 1, n + 1) {
    res *= i;
    res %= MOD;
  }
  return res * finv[k] % MOD;
}
// powMod(a,b,MOD)
// O(log(b))?
// cout << "Case #" << index << " :IMPOSSIBLE";
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll k;
  COMinit();
  string s;
  cin >> k >> s;
  ll n = s.size();
  ll res = 0;
  rep(i, k + 1) {
    ll g = COM(n + k - 1 - i, n - 1);
    ll f = powMod(25, k - i, MOD) * powMod(26, i, MOD);
    if (f >= MOD)
      f %= MOD;
    ll h = f * g;
    if (h >= MOD)
      h %= MOD;
    res += h;
    if (res >= MOD)
      res %= MOD;
  }
  cout << res << endl;
}