Problem p02632 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

const int MAX = 5000000;

int fac[MAX], finv[MAX], inv[MAX];

void t() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  repi(i, 2, MAX) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int calc(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;
}

int modinv(int a, int m) {
  int b = m, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int k;
  string s;
  cin >> k >> s;
  int num = s.size();
  int ans = 0;
  int now = 1;
  rep(i, k) {
    now *= 25;
    now %= MOD;
  }
  t();
  rep(i, k + 1) {

    if (i != 0) {
      now = now * modinv(25, MOD) % MOD;
      now *= 26;
      now %= MOD;
    }
    // cout<<now<<" "<<ans<<endl;
    int kosuu = num - 1 + (k - i);
    int pl = now * calc(kosuu, k - i) % MOD;
    ans += pl;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

const int MAX = 500000;

int fac[MAX], finv[MAX], inv[MAX];

void t() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  repi(i, 2, MAX) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int calc(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;
}

int modinv(int a, int m) {
  int b = m, u = 1, v = 0;
  while (b) {
    int t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int k;
  string s;
  cin >> k >> s;
  int num = s.size();
  int ans = 0;
  int now = 1;
  rep(i, k) {
    now *= 25;
    now %= MOD;
  }
  t();
  rep(i, k + 1) {

    if (i != 0) {
      now = now * modinv(25, MOD) % MOD;
      now *= 26;
      now %= MOD;
    }
    // cout<<now<<" "<<ans<<endl;
    int kosuu = num - 1 + (k - i);
    int pl = now * calc(kosuu, k - i) % MOD;
    ans += pl;
    ans %= MOD;
  }

  cout << ans << endl;

  return 0;
}