Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define DEB2(X) cout << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define DEB2(X)                                                                \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
// typedef std::pair<int,int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define NP(X, Y) next_permutation(X, Y)
#define setdouble(X, Y) cout << fixed << setprecision(X) << Y
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int K;
string S;
int A[1000010], B[1000010];

void mul(int &a, int b) {
  a *= b;
  a %= MOD;
}

void add(int &a, int b) {
  a += b;
  a %= MOD;
}

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

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

// 階乗
std::vector<int> fact;
void mod_fact(int n, int m) {
  fact.resize(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % m;
  }
}

// nCk mod p
int mod_nCr(int n, int k, int m) {
  if (n < k)
    return 0;
  return fact[n] * mod_inverse(fact[k] * fact[n - k] % m, m) % m;
}

signed main() {
  cin >> K >> S;
  int N = S.size();
  mod_fact(1000010, MOD);
  for (int i = 0; i <= 1000000; i++) {
    A[i] = 1, B[i] = 1;
  }
  for (int i = 1; i <= 1000000; i++) {
    A[i] = A[i - 1] * 26 % MOD;
    B[i] = B[i - 1] * 25 % MOD;
  }
  int ans = 0;
  for (int i = 0; i <= K; i++) {
    int res = mod_nCr(N - 1 + K - i, K - i, MOD);
    mul(res, A[i]);
    mul(res, B[K - i]);
    add(ans, res);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define DEB2(X) cout << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define DEB2(X)                                                                \
  {}
#define END                                                                    \
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
// typedef std::pair<int,int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define NP(X, Y) next_permutation(X, Y)
#define setdouble(X, Y) cout << fixed << setprecision(X) << Y
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int K;
string S;
int A[1000010], B[1000010];

void mul(int &a, int b) {
  a *= b;
  a %= MOD;
}

void add(int &a, int b) {
  a += b;
  a %= MOD;
}

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

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

// 階乗
std::vector<int> fact;
void mod_fact(int n, int m) {
  fact.resize(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % m;
  }
}

// nCk mod p
int mod_nCr(int n, int k, int m) {
  if (n < k)
    return 0;
  return fact[n] * mod_inverse(fact[k] * fact[n - k] % m, m) % m;
}

signed main() {
  cin >> K >> S;
  int N = S.size();
  mod_fact(5000010, MOD);
  for (int i = 0; i <= 1000000; i++) {
    A[i] = 1, B[i] = 1;
  }
  for (int i = 1; i <= 1000000; i++) {
    A[i] = A[i - 1] * 26 % MOD;
    B[i] = B[i - 1] * 25 % MOD;
  }
  int ans = 0;
  for (int i = 0; i <= K; i++) {
    int res = mod_nCr(N - 1 + K - i, K - i, MOD);
    mul(res, A[i]);
    mul(res, B[K - i]);
    add(ans, res);
  }
  cout << ans << endl;
  return 0;
}