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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9 + 7;
const int maxn = 2000000 + 10;

ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    b >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return res;
}

ll fac[maxn], caf[maxn]; // factorial e inverso

ll deg(ll x, ll d) {
  d %= mod - 1;
  if (d < 0)
    d += mod - 1;
  ll y = 1;
  while (d) {
    if (d & 1)
      y *= x, y %= mod;
    x *= x, x %= mod;
    d /= 2;
  }
  return y;
}
ll coeff(int n, int k) { return fac[n] * caf[k] % mod * caf[n - k] % mod; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fac[0] = caf[0] = 1;
  forn(i, 1, maxn) fac[i] = i * fac[i - 1] % mod, caf[i] = deg(fac[i], -1);

  string str;
  int N, K;
  cin >> K >> str;
  N = str.size() + K;
  ll res = 0;
  forn(i, str.size(), N + 1) {
    int cnt25 = i - (int)str.size();
    int cnt26 = N - i;
    ll tmp = ((power(25, cnt25)) * (power(26, cnt26))) % mod;
    tmp = ((tmp) * (coeff(i - 1, str.size() - 1))) % mod;
    res += tmp;
    res %= mod;
  }

  if (res < 0)
    res += mod;
  cout << res << '\\n';

  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mod(n, k) ((((n) % (k)) + (k)) % (k))
#define forn(i, a, b) for (int i = a; i < b; i++)
#define forr(i, a, b) for (int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9 + 7;
const int maxn = 50000;
ll memo[maxn][20];
string str;
int N, K, S;

ll dp(int pos, int pos2) {
  if (pos >= N) {
    if (pos2 >= (int)str.size())
      return 1;
    return 0;
  }
  ll &res = memo[pos][pos2];
  if (res != -1)
    return res;
  res = 0;
  for (char c = 'a'; c <= 'z'; c++) {
    if (pos2 < (int)str.size()) {
      res += dp(pos + 1, pos2 + (str[pos2] == c));
      res %= mod;
    } else {
      res += dp(pos + 1, pos2);
      res %= mod;
    }
  }
  return res;
}
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    b >>= 1;
    a = ((a % mod) * (a % mod)) % mod;
  }
  return res;
}
ll gcdE(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdE(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
ll modInverse(ll b, ll m) {
  ll x, y;
  ll g = gcdE(b, m, &x, &y);
  if (g != 1)
    return -1;
  return (x % mod + mod) % mod;
}
ll modDivide(ll a, ll b) {
  a = a % mod;
  ll inv = modInverse(b, mod);
  if (inv == -1)
    return -1;
  return (inv * a) % mod;
}
ll binomialCoeff(int n, int k) {
  ll res = 1;
  if (k > n - k)
    k = n - k;
  for (int i = 0; i < k; i++) {
    res = (res % mod * (n - i) % mod) % mod;
    res = modDivide(res, i + 1);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // forn(i,0,maxn)forn(j,0,20)memo[i][j] = -1;
  cin >> K >> str;
  N = str.size() + K;
  // ll res = dp(0,0);
  // if(res < 0)res += mod;
  // cout << res << '\\n';
  ll res = 0;
  forn(i, str.size(), N + 1) {
    int cnt25 = i - (int)str.size();
    int cnt26 = N - i;
    // cout << i << ' ' << cnt25 << ' ' << cnt26 << '\\n';
    ll tmp = ((power(25, cnt25)) * (power(26, cnt26))) % mod;
    tmp = ((tmp) * (binomialCoeff(i - 1, str.size() - 1))) % mod;
    // cout << tmp << '\\n';
    res += tmp;
    res %= mod;
  }

  if (res < 0)
    res += mod;
  cout << res << '\\n';

  return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/