Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
vll fac(2, 1), inv(2, 1), finv(2, 1);

void nCr_set(int n) {
  for (int i = 2; i <= n; i++) {
    fac.push_back(fac.at(i - 1) * i % mod);
    inv.push_back(mod - inv.at(mod % i) * (mod / i) % mod);
    finv.push_back(finv.at(i - 1) * inv.at(i) % mod);
  }
}

ll nCr(ll n, ll r) {
  if (n < 0 || r < 0 || n < r)
    return 0;
  if (n == r || r == 0)
    return 1;
  return fac.at(n) * finv.at(n - r) % mod * finv.at(r) % mod;
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n - 1); }

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

int main() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.size();
  ll ans = 0;
  nCr_set(3 * 1e5);
  for (int i = 0; i <= k; i++) {
    ans += nCr(n + k - i - 1, n - 1) * modpow(25, k - i) % mod * modpow(26, i) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pll = pair<ll, ll>;
#define all(v) v.begin(), v.end()
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const double pie = acos(-1);
vll dx4 = {-1, 0, 1, 0};
vll dy4 = {0, -1, 0, 1};
vll dx8 = {-1, 0, 1, 1, 1, 0, -1, -1};
vll dy8 = {-1, -1, -1, 0, 1, 1, 1, 0};

void fix_cout() { cout << fixed << setprecision(20); }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
vll fac(2, 1), inv(2, 1), finv(2, 1);

void nCr_set(int n) {
  for (int i = 2; i <= n; i++) {
    fac.push_back(fac.at(i - 1) * i % mod);
    inv.push_back(mod - inv.at(mod % i) * (mod / i) % mod);
    finv.push_back(finv.at(i - 1) * inv.at(i) % mod);
  }
}

ll nCr(ll n, ll r) {
  if (n < 0 || r < 0 || n < r)
    return 0;
  if (n == r || r == 0)
    return 1;
  return fac.at(n) * finv.at(n - r) % mod * finv.at(r) % mod;
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n - 1); }

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

int main() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.size();
  ll ans = 0;
  nCr_set(3 * 1e6);
  for (int i = 0; i <= k; i++) {
    ans += nCr(n + k - i - 1, n - 1) * modpow(25, k - i) % mod * modpow(26, i) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}