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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
// #define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll bin(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return res % mod;
}

ll inv(const ll &a) { return bin(a, mod - 2) % mod; }

ll fact[2000005];
ll inverse[2000005];

ll ncr(ll n, ll r) {
  if (r == 0 || n == r)
    return 1;

  return ((fact[n] % mod) *
          (((inverse[r] % mod) * (inverse[n - r] % mod) % mod)) % mod) %
         mod;
}

int32_t main() {
  fastio fact[0] = 1;
  for (ll i = 1; i <= 2000000; ++i)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  for (ll i = 0; i <= 2000000; ++i)
    inverse[i] = inv(fact[i]) % mod;

  ll k, n;
  string s;
  cin >> k >> s;
  n = s.length();

  ll ans = bin(26, k + n) % mod;
  ll ans1 = 0;
  for (ll i = 0; i < n; ++i) {
    ans1 = ans1 + ((ncr(n + k, i) * bin(25, n + k - i)) % mod);
    ans1 %= mod;
  }

  cout << ((ans - ans1 % mod) + mod) % mod << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
// #define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll bin(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = ((res % mod) * (a % mod)) % mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return res % mod;
}

ll inv(const ll &a) { return bin(a, mod - 2) % mod; }

ll fact[200005];
ll inverse[200005];

ll ncr(ll n, ll r) {
  if (r == 0 || n == r)
    return 1;

  return ((fact[n] % mod) *
          (((inverse[r] % mod) * (inverse[n - r] % mod) % mod)) % mod) %
         mod;
}

int32_t main() {
  fastio fact[0] = 1;
  for (ll i = 1; i <= 200000; ++i)
    fact[i] = fact[i - 1] * i, fact[i] %= mod;
  for (ll i = 0; i <= 200000; ++i)
    inverse[i] = inv(fact[i]) % mod;

  ll k, n;
  string s;
  cin >> k >> s;
  n = s.length();

  ll ans = bin(26, k + n);
  ll ans1 = 0;
  for (ll i = 0; i < n; ++i) {
    ans1 += ncr(n + k, i) * (bin(25, n + k - i));
    ans1 %= mod;
  }

  cout << (ans - ans1 + mod) % mod << endl;
}