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

#line 1 "main.cpp"
#line 1 "main.cpp"

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) {}
    constexpr void operator++() noexcept { ++i; }
    constexpr itr operator*() const noexcept { return i; }
    constexpr bool operator!=(iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) {}
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) {}
    constexpr void operator++() noexcept { --i; }
    constexpr itr operator*() const noexcept { return i; }
    constexpr bool operator!=(iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept
      : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

#line 2 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/modular.cpp"

#include <cstdint>
#line 5 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/modular.cpp"

template <uint32_t Modulus> class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type get_mod() noexcept { return mod; }
  static_assert(mod >= 2, "invalid mod :: smaller than 2");
  static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");

  template <class T> static constexpr value_type normalize(T value_) noexcept {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0)
        return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular() noexcept : value(0) {}
  template <class T>
  explicit constexpr modular(T value_) noexcept : value(normalize(value_)) {}
  template <class T> explicit constexpr operator T() const noexcept {
    return static_cast<T>(value);
  }

  constexpr value_type get() const noexcept { return value; }
  constexpr modular operator-() const noexcept { return modular(mod - value); }
  constexpr modular operator~() const noexcept { return inverse(); }

  constexpr value_type &extract() noexcept { return value; }
  constexpr modular inverse() const noexcept { return power(mod - 2); }
  constexpr modular power(max_type exp) const noexcept {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1)
        res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator+(const modular &rhs) const noexcept {
    return modular(*this) += rhs;
  }
  constexpr modular &operator+=(const modular &rhs) noexcept {
    if ((value += rhs.value) >= mod)
      value -= mod;
    return *this;
  }

  constexpr modular operator-(const modular &rhs) const noexcept {
    return modular(*this) -= rhs;
  }
  constexpr modular &operator-=(const modular &rhs) noexcept {
    if ((value += mod - rhs.value) >= mod)
      value -= mod;
    return *this;
  }

  constexpr modular operator*(const modular &rhs) const noexcept {
    return modular(*this) *= rhs;
  }
  constexpr modular &operator*=(const modular &rhs) noexcept {
    value = (max_type)value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator/(const modular &rhs) const noexcept {
    return modular(*this) /= rhs;
  }
  constexpr modular &operator/=(const modular &rhs) noexcept {
    return (*this) *= rhs.inverse();
  }

  constexpr bool zero() const noexcept { return value == 0; }
  constexpr bool operator==(const modular &rhs) const noexcept {
    return value == rhs.value;
  }
  constexpr bool operator!=(const modular &rhs) const noexcept {
    return value != rhs.value;
  }
  friend std::ostream &operator<<(std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }
};
#line 2 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/factorials.cpp"

#include <cstddef>
#line 5 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/factorials.cpp"

template <class T, size_t N> class factorials {
public:
  using value_type = T;
  static constexpr size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator()(size_t n, size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }
};
#line 53 "main.cpp"

using m32 = modular<1000000007>;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

factorials<m32, 2000000> fact;

int main() {
  size_t K;
  std::cin >> K;
  std::string S;
  std::cin >> S;
  size_t N = S.size();
  m32 ans;
  for (auto i : range(N - 1, N + K)) {
    size_t back = N + K - i - 1;
    ans += fact(i, N - 1) * m32(25).power(K - back) * m32(26).power(back);
  }
  std::cout << ans << '\\n';
  return 0;
}


Error: WA

Fixed submission:

#line 1 "main.cpp"

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) {}
    constexpr void operator++() noexcept { ++i; }
    constexpr itr operator*() const noexcept { return i; }
    constexpr bool operator!=(iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_) noexcept : l(l_), r(std::max(l_, r_)) {}
  constexpr iterator begin() const noexcept { return l; }
  constexpr iterator end() const noexcept { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_) noexcept : i(i_) {}
    constexpr void operator++() noexcept { --i; }
    constexpr itr operator*() const noexcept { return i; }
    constexpr bool operator!=(iterator x) const noexcept { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_) noexcept
      : l(l_ - 1), r(std::max(l_, r_) - 1) {}
  constexpr iterator begin() const noexcept { return r; }
  constexpr iterator end() const noexcept { return l; }
};

#line 2 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/modular.cpp"

#include <cstdint>
#line 5 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/modular.cpp"

template <uint32_t Modulus> class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static constexpr value_type get_mod() noexcept { return mod; }
  static_assert(mod >= 2, "invalid mod :: smaller than 2");
  static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");

  template <class T> static constexpr value_type normalize(T value_) noexcept {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0)
        return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular() noexcept : value(0) {}
  template <class T>
  explicit constexpr modular(T value_) noexcept : value(normalize(value_)) {}
  template <class T> explicit constexpr operator T() const noexcept {
    return static_cast<T>(value);
  }

  constexpr value_type get() const noexcept { return value; }
  constexpr modular operator-() const noexcept { return modular(mod - value); }
  constexpr modular operator~() const noexcept { return inverse(); }

  constexpr value_type &extract() noexcept { return value; }
  constexpr modular inverse() const noexcept { return power(mod - 2); }
  constexpr modular power(max_type exp) const noexcept {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1)
        res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator+(const modular &rhs) const noexcept {
    return modular(*this) += rhs;
  }
  constexpr modular &operator+=(const modular &rhs) noexcept {
    if ((value += rhs.value) >= mod)
      value -= mod;
    return *this;
  }

  constexpr modular operator-(const modular &rhs) const noexcept {
    return modular(*this) -= rhs;
  }
  constexpr modular &operator-=(const modular &rhs) noexcept {
    if ((value += mod - rhs.value) >= mod)
      value -= mod;
    return *this;
  }

  constexpr modular operator*(const modular &rhs) const noexcept {
    return modular(*this) *= rhs;
  }
  constexpr modular &operator*=(const modular &rhs) noexcept {
    value = (max_type)value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator/(const modular &rhs) const noexcept {
    return modular(*this) /= rhs;
  }
  constexpr modular &operator/=(const modular &rhs) noexcept {
    return (*this) *= rhs.inverse();
  }

  constexpr bool zero() const noexcept { return value == 0; }
  constexpr bool operator==(const modular &rhs) const noexcept {
    return value == rhs.value;
  }
  constexpr bool operator!=(const modular &rhs) const noexcept {
    return value != rhs.value;
  }
  friend std::ostream &operator<<(std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }
};
#line 2 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/factorials.cpp"

#include <cstddef>
#line 5 "/Users/kodamankod/Desktop/Programming/Libraries/algebraic/factorials.cpp"

template <class T, size_t N> class factorials {
public:
  using value_type = T;
  static constexpr size_t size = N;

public:
  std::array<value_type, size + 1> fact{};
  std::array<value_type, size + 1> fact_inv{};

  factorials() {
    fact.front() = value_type(1);
    for (size_t i = 1; i <= size; ++i) {
      fact[i] = fact[i - 1] * value_type(i);
    }
    fact_inv.back() = ~fact.back();
    for (size_t i = size; i > 0; --i) {
      fact_inv[i - 1] = fact_inv[i] * value_type(i);
    }
  }

  value_type operator()(size_t n, size_t r) const {
    return fact[n] * fact_inv[n - r] * fact_inv[r];
  }
};
#line 53 "main.cpp"

using m32 = modular<1000000007>;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

factorials<m32, 1000000> fact;

int main() {
  size_t K;
  std::cin >> K;
  std::string S;
  std::cin >> S;
  size_t N = S.size();
  m32 ans;
  for (auto i : range(N, N + K + 1)) {
    ans += fact(N + K, i) * m32(25).power(N + K - i);
  }
  std::cout << ans + m32(1) << '\\n';
  return 0;
}