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

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
Apparently, the tree supports the same operations as the set
(at least I haven't any problems with them before), but also there are two new
features — find_by_order() — The first returns an iterator to the k-th largest
element (counting from zero) order_of_key() — the number of items in a set that
are strictly smaller than our item. https://codeforces.com/blog/entry/11080
using namespace __gnu_pbds; */
using namespace std;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int test = 1; test <= t; test++)
#define int long long
#define MOD 1000000007
#define pb push_back
#define ins insert
#define fast()                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ld long double
#define endl '\\n'
int m(int a) { return ((a % MOD) + MOD) % MOD; }
int p(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = m(m(res) * m(a));
    a = m(m(a) * m(a));
    b >>= 1;
  }
  return res;
}
int inv(int n) { return p(n, MOD - 2); }
vector<int> fac;
int nCr(int n, int r) {
  if (r == 0)
    return 1;
  return m(m(m(fac[n]) * m(inv(fac[r]))) * m(inv(fac[n - r])));
}
signed main() {
  fast();
  string s;
  int k;
  cin >> k;
  cin >> s;
  int n = k + s.length();
  fac.resize(n + 1);
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = m(m(fac[i - 1]) * m(i));
  int z = 26;
  int ans = p(z, n), f = 0;
  for (int i = 0; i < s.length(); i++) {
    f = m(m(nCr(n, i)) * m(p(z - 1, n - i)));
    ans = m(m(ans) - m(f));
  }
  // cout << f << endl;
  cout << ans << endl;
  cerr << "\\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\\n";
}

Error: 0

Time elapsed : 100 ms


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
Apparently, the tree supports the same operations as the set
(at least I haven't any problems with them before), but also there are two new
features — find_by_order() — The first returns an iterator to the k-th largest
element (counting from zero) order_of_key() — the number of items in a set that
are strictly smaller than our item. https://codeforces.com/blog/entry/11080
using namespace __gnu_pbds; */
using namespace std;
#define test()                                                                 \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  for (int test = 1; test <= t; test++)
#define int long long
#define MOD 1000000007
#define pb push_back
#define ins insert
#define fast()                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ld long double
#define endl '\\n'
int m(int a) { return ((a % MOD) + MOD) % MOD; }
int p(int a, int b) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = m(m(res) * m(a));
    a = m(m(a) * m(a));
    b >>= 1;
  }
  return res;
}
int inv(int n) { return p(n, MOD - 2); }
vector<int> fact;
int nCr(int n, int r) {
  // if(r == 0)  return 1;
  return m(m(m(fact[n]) * m(inv(fact[r]))) * m(inv(fact[n - r])));
}
signed main() {
  fast();
  fact.resize(1000001);
  fact[0] = 1;
  for (int i = 1; i <= 1000000; i++) {
    fact[i] = m(m(fact[i - 1]) * m(i));
  }
  // for(int i=1; i<=10; i++)    cerr << fact[i] << " ";
  // cerr << endl;
  string s;
  int k;
  cin >> k;
  cin >> s;
  int n = k + s.length();
  int z = 26;
  int ans = p(z, n), f = 0;
  for (int i = 0; i < s.length(); i++) {
    f = m(m(nCr(n, i)) * m(p(z - 1, n - i)));
    ans = m(m(ans) - m(f));
  }
  // cout << f << endl;
  cout << ans << endl;
  cerr << "\\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\\n";
}