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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> ii;
typedef pair<int64, int64> int64int64;
typedef vector<int> vint;
typedef vector<int64> vint64;
typedef vector<uint64> vuint64;
typedef vector<vint> vvint;
typedef vector<vint64> vvint64;
typedef vector<vuint64> vvuint64;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;

#define mp make_pair
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define INF9 (int64)(1e9)
#define INF18 (int64)(1e18)

template <typename T> struct compF {
  bool operator()(const pair<T, T> &a, const pair<T, T> &b) {
    return a.first < b.first;
  }
};

template <typename T> struct compS {
  bool operator()(const pair<T, T> &a, const pair<T, T> &b) {
    return a.second < b.second;
  }
};

template <typename T>
void printMatrix(const string &txt, vector<vector<T>> &a) {
  cout << txt;
  fori(a.size()) {
    forj(a[i].size()) cout << a[i][j] << ' ';

    cout << endl;
  }
}

template <typename T> void printVector(const string &txt, vector<T> &a) {
  cout << txt;
  for (int i = 0; i < a.size(); ++i)
    cout << a[i] << ' ';
  cout << endl;
}

template <typename T> void readVector(int n, vector<T> &a) {
  if (a.size() < n)
    a.resize(n);

  fori(n) cin >> a[i];
}

template <typename T1, typename T2> bool exist(map<T1, T2> &m, T1 x) {
  if (m.find(x) == m.end())
    return false;

  return true;
}

int64 gcd(int64 a, int64 b) {
  while (a != 0 && b != 0) {
    if (b > a)
      b %= a;
    else
      a %= b;
  }

  return a + b;
}

int64 minFactor(int64 x) {
  for (int64 i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return i;

  return x;
}

int64 allFactors(int64 x, vint64 &prms, vint64 &prm_cnt) {
  prms.clear();
  prm_cnt.clear();
  while (x > 1) {
    int64 t = minFactor(x);
    prms.push_back(t);
    int s = prms.size() - 1;
    prm_cnt.push_back(0);
    while (x % t == 0) {
      x /= t;
      ++prm_cnt[s];
    }
  }

  return 0;
}

#define MOD (int64)(1000000007)

int64 add(int64 x, int64 y) { return ((x % MOD) + (y % MOD)) % MOD; }

int64 mul(int64 x, int64 y) {
  if (y == 0 || x == 0)
    return 0;

  if (y % 2 == 0) {
    int64 k = mul(x, y / 2);
    return (k * 2) % MOD;
  } else
    return add(x, mul(x, y - 1));
}

int64 mul_(int64 x, int64 y) { return ((x % MOD) * (y % MOD)) % MOD; }

int64 pow_(int64 x, int64 y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0) {
    int64 k = pow_(x, y / 2);
    return (k * k) % MOD;
  } else
    return (x * pow_(x, y - 1)) % MOD;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie();

  int k;
  string s;

  cin >> k >> s;
  vint64 fact(2 * ((int)1e6 + 2));
  fact[0] = 1;
  fact[1] = 1;

  for (int i = 2; i < fact.size(); ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
    // if (i < 20) cout << fact[i] << endl;
  }

  vint64 revfact(fact.size());
  fori(revfact.size() - 1) revfact[i] = pow_(fact[i], MOD - 2);

  int n = s.length() + k;
  int64 total = pow_(26, n);
  int64 sum = 0;
  for (int i = 0; i < s.length(); ++i) {
    int64 t = pow_(25, n - i);
    int64 comb = mul(fact[n], mul_(revfact[i], revfact[n - i]));
    sum = add(sum, mul_(t, comb));
  }

  int64 res = total - sum;
  while (res < 0)
    res += MOD;

  cout << res << endl;
  return 0;
}


Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> ii;
typedef pair<int64, int64> int64int64;
typedef vector<int> vint;
typedef vector<int64> vint64;
typedef vector<uint64> vuint64;
typedef vector<vint> vvint;
typedef vector<vint64> vvint64;
typedef vector<vuint64> vvuint64;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;

#define mp make_pair
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define fori(n) for (int i = 0; i < n; ++i)
#define forj(n) for (int j = 0; j < n; ++j)
#define fork(n) for (int k = 0; k < n; ++k)
#define INF9 (int64)(1e9)
#define INF18 (int64)(1e18)

template <typename T> struct compF {
  bool operator()(const pair<T, T> &a, const pair<T, T> &b) {
    return a.first < b.first;
  }
};

template <typename T> struct compS {
  bool operator()(const pair<T, T> &a, const pair<T, T> &b) {
    return a.second < b.second;
  }
};

template <typename T>
void printMatrix(const string &txt, vector<vector<T>> &a) {
  cout << txt;
  fori(a.size()) {
    forj(a[i].size()) cout << a[i][j] << ' ';

    cout << endl;
  }
}

template <typename T> void printVector(const string &txt, vector<T> &a) {
  cout << txt;
  for (int i = 0; i < a.size(); ++i)
    cout << a[i] << ' ';
  cout << endl;
}

template <typename T> void readVector(int n, vector<T> &a) {
  if (a.size() < n)
    a.resize(n);

  fori(n) cin >> a[i];
}

template <typename T1, typename T2> bool exist(map<T1, T2> &m, T1 x) {
  if (m.find(x) == m.end())
    return false;

  return true;
}

int64 gcd(int64 a, int64 b) {
  while (a != 0 && b != 0) {
    if (b > a)
      b %= a;
    else
      a %= b;
  }

  return a + b;
}

int64 minFactor(int64 x) {
  for (int64 i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return i;

  return x;
}

int64 allFactors(int64 x, vint64 &prms, vint64 &prm_cnt) {
  prms.clear();
  prm_cnt.clear();
  while (x > 1) {
    int64 t = minFactor(x);
    prms.push_back(t);
    int s = prms.size() - 1;
    prm_cnt.push_back(0);
    while (x % t == 0) {
      x /= t;
      ++prm_cnt[s];
    }
  }

  return 0;
}

#define MOD (int64)(1000000007)

int64 add(int64 x, int64 y) { return ((x % MOD) + (y % MOD)) % MOD; }

uint64 mul(uint64 x, uint64 y) {
  if (y == 0 || x == 0)
    return 0;

  if (y % 2 == 0) {
    int64 k = mul(x, y / 2);
    return (k * 2) % MOD;
  } else
    return (x + mul(x, y - 1)) % MOD;
}

int64 pow_(int64 x, int64 y) {
  if (y == 0)
    return 1;
  if (y % 2 == 0) {
    int64 k = pow_(x, y / 2);
    return (k * k) % MOD;
  } else
    return (x * pow_(x, y - 1)) % MOD;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie();

  int k;
  string s;

  cin >> k >> s;
  vint64 fact((int)1e5 + 2);
  fact[0] = 1;
  fact[1] = 1;

  for (int i = 2; i <= 1e5; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD;
    // if (i < 20) cout << fact[i] << endl;
  }

  vint64 revfact((int(1e5)) + 2);
  fori(1e5 + 1) revfact[i] = pow_(fact[i], MOD - 2);

  int n = s.length() + k;
  int64 total = pow_(26, n);
  int64 sum = 0;
  for (int i = 0; i < s.length(); ++i) {
    int64 t = pow_(25, n - i);
    int64 comb = mul(fact[n], mul(revfact[i], revfact[n - i]));
    sum = add(sum, mul(t, comb));
  }

  int64 res = total - sum;
  while (res < 0)
    res += MOD;

  cout << res << endl;
  return 0;
}