Problem p02632 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;
#define N 1000005
int fact[N], invfact[N];

void fun() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  invfact[N - 1] = modinv(fact[N - 1]);
  for (int i = N - 2; i >= 0; i--)
    invfact[i] = mul(invfact[i + 1], i + 1);
}

int ncr(int n, int r) {
  int ans = fact[n];
  ans = mul(ans, invfact[n - r]);
  ans = mul(ans, invfact[r]);
  return ans;
}

void solve() {
  fun();
  int k, ans = 0;
  string s;
  cin >> k >> s;
  int n = s.length(), i;
  int m = n + k;
  for (i = n; i <= m; i++) {
    int x = power(26, m - i);
    int y = power(25, i - n);
    int z = ncr(i - 1, n - 1);
    int val = mul(x, mul(y, z));
    ans = add(ans, val);
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pr pair<int, int>
#define mod 1000000007
#define endl "\n"

int power(int a, int b, int m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  int res = power(a, b / 2, m);
  res = (res * res) % m;
  if (b & 1)
    res = (res * a) % m;
  return res;
}
int modinv(int a, int m = mod) { return power(a, m - 2, m); }
int add(int a, int b, int m = mod) {
  int c = (a % m + b % m);
  if (c >= m)
    c -= m;
  return c;
}
int sub(int a, int b, int m = mod) {
  int c = (a % m - b % m);
  if (c < 0)
    c += m;
  return c;
}
int mul(int a, int b, int m = mod) { return (a * b) % m; }
//	x<<1 => x*2		x>>1 => x/2;	cout<<flush;
//	cout<<fixed<<setprecision(10)<<ans<<endl;
#define N 2000005
int fact[N], invfact[N];

void fun() {
  fact[0] = 1;
  for (int i = 1; i < N; i++)
    fact[i] = mul(fact[i - 1], i);
  invfact[N - 1] = modinv(fact[N - 1]);
  for (int i = N - 2; i >= 0; i--)
    invfact[i] = mul(invfact[i + 1], i + 1);
}

int ncr(int n, int r) {
  int ans = fact[n];
  ans = mul(ans, invfact[n - r]);
  ans = mul(ans, invfact[r]);
  return ans;
}

void solve() {
  fun();
  int k, ans = 0;
  string s;
  cin >> k >> s;
  int n = s.length(), i;
  int m = n + k;
  for (i = n; i <= m; i++) {
    int x = power(26, m - i);
    int y = power(25, i - n);
    int z = ncr(i - 1, n - 1);
    int val = mul(x, mul(y, z));
    ans = add(ans, val);
  }
  cout << ans << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1; // cin>>t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}