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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  long long x;
  mint(long long x = 0) : x(x % MOD + (0 <= x ? 0 : MOD)) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x -= a.x) < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(x) += a; }
  mint operator-(const mint &a) const { return mint(x) -= a; }
  mint operator*(const mint &a) const { return mint(x) *= a; }

  // for prime mod
  mint inv() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return mint(u);
  }
  mint &operator/=(const mint &a) { return *this *= a.inv(); }
  mint operator/(const mint &a) const { return mint(x) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct Combination {
  vector<long long> fac, finv, inv;
  Combination(int n = 1001001) : fac(n + 1), finv(n + 1), inv(n + 1) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i <= n; 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;
    }
  }
  long long operator()(int n, int k) const {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
  }
};

long long mod_pow(long long x, long long n, int M = 1000000007) {
  long long ret = 1;
  while (n) {
    if (n & 1)
      (ret *= x) %= M;
    (x *= x) %= M;
    n >>= 1;
  }
  return ret;
}

int main() {
  int K;
  string S;
  cin >> K >> S;

  int N = S.size();

  mint ans = 0;
  Combination c(2001001);

  for (int i = N; i <= K + N; ++i) {
    int left = i - N;
    int n = left + 1, r = N - 1;
    mint tmp = (mint)mod_pow(25, left) * c(n + r - 1, r);

    int right = K + N - i;
    ans += tmp * mod_pow(26, right);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  long long x;
  mint(long long x = 0) : x(x % MOD + (0 <= x ? 0 : MOD)) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x -= a.x) < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(x) += a; }
  mint operator-(const mint &a) const { return mint(x) -= a; }
  mint operator*(const mint &a) const { return mint(x) *= a; }

  // for prime mod
  mint inv() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return mint(u);
  }
  mint &operator/=(const mint &a) { return *this *= a.inv(); }
  mint operator/(const mint &a) const { return mint(x) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

struct Combination {
  vector<long long> fac, finv, inv;
  Combination(int n = 1001001) : fac(n + 1), finv(n + 1), inv(n + 1) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i <= n; 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;
    }
  }
  long long operator()(int n, int k) const {
    if (n < k || n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
  }
};

long long mod_pow(long long x, long long n, int M = 1000000007) {
  long long ret = 1;
  while (n) {
    if (n & 1)
      (ret *= x) %= M;
    (x *= x) %= M;
    n >>= 1;
  }
  return ret;
}

int main() {
  int K;
  string S;
  cin >> K >> S;

  int N = S.size();

  mint ans = 0;
  Combination c;

  for (int i = N; i <= K + N; ++i) {
    int left = i - N;
    int n = left + 1, r = N - 1;
    mint tmp = (mint)mod_pow(25, left) * c(n + r - 1, r);

    int right = K + N - i;
    ans += tmp * mod_pow(26, right);
  }

  cout << ans << endl;
}