Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

const int MAX = 1100000;
long long fac[MAX], finv[MAX], inv[MAX];

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

long long COM(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 modpow(ll a, ll n, ll modo) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % modo;
    a = a * a % modo;
    n >>= 1;
  }
  return res;
}

int main() {
  ll k, ans = 0;
  string s;
  cin >> k >> s;
  COMinit();
  rep(i, k + 1) {
    ans += ((COM(s.size() + k - i - 1, s.size() - 1) * modpow(25, k - i, mod) %
             mod) *
            modpow(26, i, mod)) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

const int MAX = 5000000;
long long fac[MAX], finv[MAX], inv[MAX];

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

long long COM(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 modpow(ll a, ll n, ll modo) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % modo;
    a = a * a % modo;
    n >>= 1;
  }
  return res;
}

int main() {
  ll k, ans = 0;
  string s;
  cin >> k >> s;
  COMinit();
  rep(i, k + 1) {
    ans += ((COM(s.size() + k - i - 1, s.size() - 1) * modpow(25, k - i, mod) %
             mod) *
            modpow(26, i, mod)) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}