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

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <algorithm>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>

#ifndef JHELPER1_COMMON_H
#define JHELPER1_COMMON_H

typedef long long ll;
using namespace std;

constexpr ll MOD = 1000 * 1000 * 1000 + 7;

// Returns x^y modulo MOD. O(log2(y)).
ll modPow(ll x, ll y) {
  ll res = 1, a = x;
  while (y > 0) {
    if ((y & 1) > 0) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    y >>= 1;
  }
  return res;
}

// Returns a^-1 module MOD.
ll inverse(ll a) {
  /*
   * Fermat's little theorem
   * http://en.wikipedia.org/wiki/Proofs_of_Fermat's_little_theorem
   *
   * <pre>
   * a^p = a (mod p)
   * a^(p-1) = 1 (mod p)
   * ..
   * 1/a = a^(p-2) (mod p)
   * </pre>
   */
  return modPow(a, MOD - 2);
}

#endif // JHELPER1_COMMON_H

#ifndef JHELPER1_NCK_H
#define JHELPER1_NCK_H

#ifndef JHELPER1_FACTORIAL_H
#define JHELPER1_FACTORIAL_H

// return n! module MOD
// memory: O(n)
// 0! = 1, 1! = 1, 2! = 2, 3! = 6 ...
ll fact(ll n) {
  static vector<ll> facs;
  ll csize = facs.size();
  if (csize > n)
    return facs[n];

  facs.resize(n + 1);
  for (int i = csize; i <= n; ++i) {
    facs[i] = (i == 0 ? 1 : (facs[i - 1] * i)) % MOD;
  }
  return facs[n];
}

#endif // JHELPER1_FACTORIAL_H

// Returns nCk module MOD
// Memory: O(n) used in factorial
ll nCk(ll n, ll k) {
  if (k > n)
    return 0;
  if (n > 3 && k > 3 && k > n / 2) {
    k = n - k;
  }

  // n!/(n-k)!k!
  ll ret = (fact(n) * inverse(fact(n - k))) % MOD;
  return (ret * inverse(fact(k))) % MOD;
}

#endif // JHELPER1_NCK_H

typedef long long ll;
using namespace std;

class FStrivore {
public:
  void solve(std::istream &in, std::ostream &out) {
    ll K;
    string S;
    in >> K;
    in >> S;
    ll L = S.length();

    ll N = K + L;
    ll ans = 0;
    for (int i = L; i <= N; ++i) {
      ll c = nCk(i - 1, L - 1);
      c = (c * modPow(25, i - L)) % MOD;
      c = (c * modPow(26, N - i)) % MOD;
      ans = (ans + c) % MOD;
    }

    out << ans << endl;
  }
};

int main() {
  FStrivore solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include <algorithm>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <vector>

//
// Created by chelsea on 23/06/2020.
//

#ifndef JHELPER1_MODPOW_H
#define JHELPER1_MODPOW_H

#ifndef JHELPER1_COMMON_H
#define JHELPER1_COMMON_H

typedef long long ll;
using namespace std;

#endif // JHELPER1_COMMON_H

// returns x^y modulo MOD. O(log2(y)).
ll modPow(ll x, ll y, ll mod) {
  ll res = 1, a = x;
  while (y > 0) {
    if ((y & 1) > 0) {
      res = (res * a) % mod;
    }
    a = (a * a) % mod;
    y >>= 1;
  }
  return res;
}

#endif // JHELPER1_MODPOW_H

#ifndef JHELPER1_NCK_H
#define JHELPER1_NCK_H

// global variable
map<ll, vector<ll>> _nCk;

ll nCk_mod(ll n, ll k, ll MOD) {
  if (k > n)
    return 0;
  if (n > 3 && k > 3 && k > n / 2) {
    k = n - k;
  }

  if (_nCk.find(n) == _nCk.end()) {
    _nCk[n] = {1, n};
  }

  /**
   * Fermat's little theorem
   * http://en.wikipedia.org/wiki/Proofs_of_Fermat's_little_theorem
   *
   * <pre>
   * a^p = a (mod p)
   * a^(p-1) = 1 (mod p)
   * ..
   * 1/a = a^(p-2) (mod p)
   * </pre>
   */
  for (ll i = _nCk[n].size(); i < k + 1; i++) {
    ll c1 = _nCk[n][i - 1];
    ll c2 = c1 * (n - i + 1) % MOD;
    c2 = c2 * modPow(i, MOD - 2, MOD) % MOD;
    _nCk[n].push_back(c2);
  }

  return _nCk[n][k];
}

#endif // JHELPER1_NCK_H

typedef long long ll;
using namespace std;

ll MOD = 1000 * 1000 * 1000 + 7;

class FStrivore {
public:
  void solve(std::istream &in, std::ostream &out) {
    ll K;
    string S;
    in >> K;
    in >> S;
    ll L = S.length();

    ll N = K + L;
    ll ans = 0;
    for (int i = L; i <= N; ++i) {
      ll c = nCk_mod(i - 1, L - 1, MOD);
      c *= modPow(25, i - L, MOD);
      c %= MOD;
      c *= modPow(26, N - i, MOD);
      c %= MOD;
      ans += c;
      ans %= MOD;
    }

    out << ans << endl;
  }
};

int main() {
  FStrivore solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}