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

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define all(a) a.begin(), a.end()
#define f(i, l, u) for (ll i = l; i < u; i++)

unsigned long long fac[2000001];

// A modular inverse based solution to
// compute nCr % p

/* Iterative Function to calculate (x^y)%p
in O(log y) */
unsigned long long power(unsigned long long x, ll y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
void powe(ll p) {

  fac[0] = 1;
  for (int i = 1; i <= 2000000; i++)
    fac[i] = (fac[i - 1] * i) % p;
}
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p) {
  return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

int main() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.length();
  powe(1000000007);
  ll ans = 0;
  f(i, 0, k + 1) {
    ll val = nCrModPFermat(n + k - 1 - i, n - 1, 1000000007);
    val = (((val * power(25, k - i, 1000000007)) % 1000000007) *
           (power(26, i, 1000000007))) %
          1000000007;
    ans = (ans + val) % 1000000007;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pb push_back
#define all(a) a.begin(), a.end()
#define f(i, l, u) for (ll i = l; i < u; i++)

unsigned long long fac[1000001];

// A modular inverse based solution to
// compute nCr % p

/* Iterative Function to calculate (x^y)%p
in O(log y) */
unsigned long long power(unsigned long long x, ll y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}
void powe(ll p) {

  fac[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    fac[i] = (fac[i - 1] * i) % p;
}
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p) {
  return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

int main() {
  ll k;
  cin >> k;
  string s;
  cin >> s;
  ll n = s.length();
  powe(1000000007);
  ll ans = 0;
  f(i, 0, k + 1) {
    ll val = nCrModPFermat(n + k - 1 - i, n - 1, 1000000007);
    val = (((val * power(25, k - i, 1000000007)) % 1000000007) *
           (power(26, i, 1000000007))) %
          1000000007;
    ans = (ans + val) % 1000000007;
  }

  cout << ans << endl;

  return 0;
}