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

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \\
  ifstream in("input.txt");                                                    \\
  ofstream out("output.txt");
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll G = 2000005;
ll a, k, mod = 1e9 + 7;
ll fac[G], inv_fac[G];

ll binpow(ll x, ll K) {
  x %= mod;
  if (K < 2)
    return K ? x : 1;
  return (binpow(x * x, K / 2) * (K & 1 ? x : 1)) % mod;
}

ll modinv(ll c) { return binpow(c, mod - 2); }

ll CizNpoK(ll N, ll K) {
  ll o = fac[N] * inv_fac[K];
  o %= mod;
  return (o * inv_fac[N - K]) % mod;
}

int main() {
  fast;
  fac[0] = 1;
  inv_fac[0] = modinv(fac[0]);
  for (ll q = 1; q < G; q++) {
    fac[q] = (fac[q - 1] * q) % mod;
    inv_fac[q] = modinv(fac[q]);
  }
  cin >> k;
  str t;
  cin >> t;
  a = t.size();
  k += a;
  ll all = binpow(26, k), pidori = 0;
  for (ll q = 0; q < a; q++) {
    pidori += CizNpoK(k, q) * binpow(25, k - q);
    pidori %= mod;
  }
  cout << (all - pidori + mod) % mod;
}

Error: TLE

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define F first
#define S second
#define vec vector
#define pb push_back
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
#define pq priority_queue
#define umap unordered_map
#define uset unordered_set
#define pii pair<int, int>
#define pnn pair<Node *, Node *>
#define uid uniform_int_distribution
#define FILE                                                                   \\
  ifstream in("input.txt");                                                    \\
  ofstream out("output.txt");
#define fast                                                                   \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cin.sync_with_stdio(0);                                                      \\
  cout.sync_with_stdio(0);
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const ll G = 2000005;
ll a, k, mod = 1e9 + 7;
ll fac[G];

ll binpow(ll x, ll K) {
  x %= mod;
  if (K < 2)
    return K ? x : 1;
  return (binpow(x * x, K / 2) * (K & 1 ? x : 1)) % mod;
}

ll modinv(ll c) { return binpow(c, mod - 2); }

ll CizNpoK(ll N, ll K) {
  ll o = fac[N] * modinv(fac[K]);
  o %= mod;
  return (o * modinv(fac[N - K])) % mod;
}

int main() {
  fast;
  fac[0] = 1;
  for (ll q = 1; q < G; q++)
    fac[q] = (fac[q - 1] * q) % mod;
  cin >> k;
  str t;
  cin >> t;
  a = t.size();
  k += a;
  ll all = binpow(26, k), pidori = 0;
  for (ll q = 0; q < a; q++) {
    pidori += CizNpoK(k, q) * binpow(25, k - q);
    pidori %= mod;
  }
  cout << (all - pidori + mod) % mod;
}