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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2000005;

const ll mod = 1000000007;

ll pow_exp(ll a, ll b, ll n) {
  a %= n;
  ll result = 1;
  while (b > 0) {
    if (b & 1)
      result = result * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return result;
}

vector<ll> factorialNumInverse(N + 1);

// array to precompute inverse of 1! to N!
vector<ll> naturalNumInverse(N + 1);

// array to store factorial of first N numbers
vector<ll> fact(N + 1);

// Function to precompute inverse of numbers
void InverseofNumber(ll p) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (int i = 2; i <= N; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

// Function to precompute inverse of factorials
void InverseofFactorial(ll p) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;

  // precompute inverse of natural numbers
  for (int i = 2; i <= N; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

// Function to calculate factorial of 1 to N
void factorial(ll p) {
  fact[0] = 1;

  // precompute factorials
  for (int i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}

// Function to return nCr % p in O(1) time
ll Binomial(ll N, ll R, ll p) {
  // n C r = n!*inverse(r!)*inverse((n-r)!)
  ll ans =
      ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  return ans;
}

int main() {
  InverseofNumber(mod);
  InverseofFactorial(mod);
  factorial(mod);
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll m = s.length();
  ll n = m + k;
  ll y = 0ll;
  for (ll i = 0; i < m; i++) {
    y = (y % mod + (Binomial(n, i, mod) * pow_exp(25, n - i, mod)) % mod) % mod;
  }
  ll ans = (pow_exp(26, n, mod) - y % mod + mod) % mod;
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 1000005;

const ll mod = 1000000007;

ll pow_exp(ll a, ll b, ll n) {
  a %= n;
  ll result = 1;
  while (b > 0) {
    if (b & 1)
      result = result * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return result;
}

vector<ll> factorialNumInverse(N + 1);

// array to precompute inverse of 1! to N!
vector<ll> naturalNumInverse(N + 1);

// array to store factorial of first N numbers
vector<ll> fact(N + 1);

// Function to precompute inverse of numbers
void InverseofNumber(ll p) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (int i = 2; i <= N; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}

// Function to precompute inverse of factorials
void InverseofFactorial(ll p) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;

  // precompute inverse of natural numbers
  for (int i = 2; i <= N; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}

// Function to calculate factorial of 1 to N
void factorial(ll p) {
  fact[0] = 1;

  // precompute factorials
  for (int i = 1; i <= N; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}

// Function to return nCr % p in O(1) time
ll Binomial(ll N, ll R, ll p) {
  // n C r = n!*inverse(r!)*inverse((n-r)!)
  ll ans =
      ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p;
  return ans;
}

int main() {
  InverseofNumber(mod);
  InverseofFactorial(mod);
  factorial(mod);
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll m = s.length();
  ll n = m + k;
  ll y = 0;
  for (ll i = 0; i < m; i++) {
    y = (y % mod +
         (Binomial(n, i, mod) % mod * pow_exp(25, n - i, mod) % mod) % mod) %
        mod;
  }
  ll ans = (pow_exp(26, n, mod) % mod - y % mod + mod) % mod;
  cout << ans << endl;
}