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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int MAX = 3e6 + 6;
const int MOD = 1e9 + 7;
int fat[MAX];

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

inline int sub(int a, int b) {
  a -= b;
  if (0 > a)
    a += MOD;
  return a;
}

inline int mult(int a, int b) { return (1LL * a * b) % MOD; }

int f_exp(int x, int e) {
  if (e <= 0)
    return 1;
  if (e & 1)
    return mult(x, f_exp(x, e - 1));
  return f_exp(mult(x, x), e / 2);
}

inline int inv(int x) { return f_exp(x, MOD - 2); }

inline int comb(int n, int k) {
  if (k > n)
    return 0;
  return mult(fat[n], mult(inv(fat[n - k]), inv(fat[k])));
}

void preprocess() {
  fat[0] = 1;
  fori(i, 1, MAX) { fat[i] = mult(fat[i - 1], i); }
}

void solve() {
  preprocess();
  int k;
  cin >> k;
  string s;
  cin >> s;
  int n = s.size();
  int t = n + k;
  int ans = 0;
  fori(i, n, t + 1) {
    int b = i - 1;
    int c = comb(b, n - 1);
    int f = t - i;
    int p = mult(f_exp(26, f), f_exp(25, t - f - n));
    ans = add(ans, mult(c, p));
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for (int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for (int i = int(ini); i >= int(lim); i--)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

const int MAX = 1e6 + 6;
const int MOD = 1e9 + 7;
int fat[MAX];

inline int add(int a, int b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}

inline int sub(int a, int b) {
  a -= b;
  if (0 > a)
    a += MOD;
  return a;
}

inline int mult(int a, int b) { return (1LL * a * b) % MOD; }

int f_exp(int x, int e) {
  if (e <= 0)
    return 1;
  if (e & 1)
    return mult(x, f_exp(x, e - 1));
  return f_exp(mult(x, x), e / 2);
}

inline int inv(int x) { return f_exp(x, MOD - 2); }

inline int comb(int n, int k) {
  if (k > n)
    return 0;
  return mult(fat[n], mult(inv(fat[n - k]), inv(fat[k])));
}

void preprocess() {
  fat[0] = 1;
  fori(i, 1, MAX) { fat[i] = mult(fat[i - 1], i); }
}

void solve() {
  preprocess();
  int k;
  cin >> k;
  string s;
  cin >> s;
  int n = s.size();
  int t = n + k;
  int ans = 0;
  fori(i, n, t + 1) {
    int b = i - 1;
    int c = comb(b, n - 1);
    int f = t - i;
    int p = mult(f_exp(26, f), f_exp(25, t - f - n));
    ans = add(ans, mult(c, p));
  }
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}