Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

const int MAX = 1000010;

ll fac[MAX], finv[MAX], inv[MAX];
ll cache25[MAX];
ll cache26[MAX];

void init_ncr() {
  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;
  }
}

ll ncr(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;
}
ll npr(ll val1, ll val2) {
  ll c = ncr(val1, val2);
  return c * fac[val2] % MOD;
}

ll p25(ll n) { return cache25[n]; }

ll p26(ll n) { return cache26[n]; }

int main() {
  init_ncr();
  // std::cout << std::fixed << std::setprecision(10);
  ll N;
  scanf("%lld", &N);

  ll c = 1;
  for (ll i = 0; i < MAX; i++) {
    cache25[i] = c;
    c = (c * 25) % MOD;
  }
  c = 1;
  for (ll i = 0; i < MAX; i++) {
    cache26[i] = c;
    c = (c * 26) % MOD;
  }

  string str;
  cin >> str;

  ll len = str.length();

  ll ans = 0;

  ll total_len = len + N;

  // N回操作する
  // len:元の文字列の長さ
  // total = N + len

  for (ll i = 0; i <= N; i++) {
    // cout << "i:" << i << endl;
    ll rest = N - i;
    ll count = ncr(rest + len - 1, len - 1);

    // cout << "(count):" << count << endl;
    // cout << "rest:" << rest << endl;

    count = count * p25(rest);
    count %= MOD;
    count = count * p26(i);
    count %= MOD;

    // cout << "count:" << count << endl;

    ans += count;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#include <iostream>
// #include <algorithm>
//  #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

const int MAX = 2000010;

ll fac[MAX], finv[MAX], inv[MAX];
ll cache25[MAX];
ll cache26[MAX];

void init_ncr() {
  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;
  }
}

ll ncr(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;
}
ll npr(ll val1, ll val2) {
  ll c = ncr(val1, val2);
  return c * fac[val2] % MOD;
}

ll p25(ll n) { return cache25[n]; }

ll p26(ll n) { return cache26[n]; }

int main() {
  init_ncr();
  // std::cout << std::fixed << std::setprecision(10);
  ll N;
  scanf("%lld", &N);

  ll c = 1;
  for (ll i = 0; i < MAX; i++) {
    cache25[i] = c;
    c = (c * 25) % MOD;
  }
  c = 1;
  for (ll i = 0; i < MAX; i++) {
    cache26[i] = c;
    c = (c * 26) % MOD;
  }

  string str;
  cin >> str;

  ll len = str.length();

  ll ans = 0;

  ll total_len = len + N;

  // N回操作する
  // len:元の文字列の長さ
  // total = N + len

  for (ll i = 0; i <= N; i++) {
    // cout << "i:" << i << endl;
    ll rest = N - i;
    ll count = ncr(rest + len - 1, len - 1);

    // cout << "(count):" << count << endl;
    // cout << "rest:" << rest << endl;

    count = count * p25(rest);
    count %= MOD;
    count = count * p26(i);
    count %= MOD;

    // cout << "count:" << count << endl;

    ans += count;
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}