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

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<pii>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  Int(x);                                                                      \\
  Int(y);
#define int3(x, y, z)                                                          \\
  Int(x);                                                                      \\
  int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)

// int dxy[5] = {0, 1, 0, -1, 0};
//  01Trie BIT CHT DFT FFT avl cmd cusum dijkstra dinic geo2 gin graph kruskal
//  lca lcm matrix ncm next_combination ppuf segtree st tmp topcoder uf vi

int fact[4400000];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int repow(int x, int n, int mod) {
  if (n == 0)
    return 1;
  int res = repow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int mod_inverse(int a, int m) {
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

int table(int n, int p) {
  int sum = 1;
  fact[0] = 1;
  Rep(i, n + 2) {
    sum *= i;
    sum %= p;
    fact[i] = sum;
  }
  return 0;
}

int mod_fact(int n, int p, int &e) {
  e = 0;
  if (n == 0)
    return 1;
  int res = mod_fact(n / p, p, e);
  e += n / p;

  if (n / p % 2 != 0)
    return res * (p - fact[n % p]) % p;
  return res * fact[n % p] % p;
}

int mod_comb(int n, int k, int p) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  int e1, e2, e3;
  int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2),
      a3 = mod_fact(n - k, p, e3);
  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

signed main() {
  table(4000000, MOD);

  Int(n);
  string st;
  cin >> st;
  int k = st.size();

  int len = n + k;

  int ans = repow(26, len, MOD);

  rep(i, k) {
    ans =
        (ans - (repow(25, len - i, MOD) * mod_comb(len, i, MOD) % MOD) + MOD) %
        MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<pii>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define int2(x, y)                                                             \\
  Int(x);                                                                      \\
  Int(y);
#define int3(x, y, z)                                                          \\
  Int(x);                                                                      \\
  int2(y, z);
#define in(x, a, b) ((a) <= (x) && (x) < (b))
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)

// int dxy[5] = {0, 1, 0, -1, 0};
//  01Trie BIT CHT DFT FFT avl cmd cusum dijkstra dinic geo2 gin graph kruskal
//  lca lcm matrix ncm next_combination ppuf segtree st tmp topcoder uf vi

int fact[222222 + 3];

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int extgcd(int a, int b, int &x, int &y) {
  int d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int repow(int x, int n, int mod) {
  if (n == 0)
    return 1;
  int res = repow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int mod_inverse(int a, int m) {
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

int table(int n, int p) {
  int sum = 1;
  fact[0] = 1;
  Rep(i, n + 2) {
    sum *= i;
    sum %= p;
    fact[i] = sum;
  }
  return 0;
}

int mod_fact(int n, int p, int &e) {
  e = 0;
  if (n == 0)
    return 1;
  int res = mod_fact(n / p, p, e);
  e += n / p;

  if (n / p % 2 != 0)
    return res * (p - fact[n % p]) % p;
  return res * fact[n % p] % p;
}

int mod_comb(int n, int k, int p) {
  if (n < 0 || k < 0 || n < k)
    return 0;
  int e1, e2, e3;
  int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2),
      a3 = mod_fact(n - k, p, e3);
  if (e1 > e2 + e3)
    return 0;
  return a1 * mod_inverse(a2 * a3 % p, p) % p;
}

signed main() {
  table(222222, MOD);

  Int(n);
  string st;
  cin >> st;
  int k = st.size();

  int len = n + k;

  int ans = repow(26, len, MOD);

  rep(i, k) {
    ans =
        (ans - (repow(25, len - i, MOD) * mod_comb(len, i, MOD) % MOD) + MOD) %
        MOD;
  }

  cout << ans << endl;

  return 0;
}