#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template T make_v(T init) { return init; } template auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec(s, v); } template struct multi_dem_array { using type = std::array::type, Head>; }; template struct multi_dem_array { using type = std::array; }; template using mdarray = typename multi_dem_array::type; namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } namespace math { template constexpr T pow(const T &n, ll k) { T ret = n.mul_id_ele(); T cur = n; while (k) { if (k & 1) ret *= cur; cur *= cur; k /= 2; } return ret; } } namespace math { template struct Modint { constexpr Modint(ll x) noexcept : x((Mod + x % Mod) % Mod) { } constexpr Modint() noexcept : Modint(0) { } constexpr Modint add_id_ele() const noexcept { return Modint(0); } constexpr Modint mul_id_ele() const noexcept { return Modint(1); } constexpr ll& value() noexcept { return x; } constexpr ll value() const noexcept { return x; } constexpr Modint& operator +=(const Modint &oth) noexcept { x += oth.value(); if (Mod <= x) x -= Mod; return *this; } constexpr Modint& operator -=(const Modint &oth) noexcept { x += Mod - oth.value(); if (Mod <= x) x -= Mod; return *this; } constexpr Modint& operator *=(const Modint &oth) noexcept { x *= oth.value(); x %= Mod; return *this; } constexpr Modint& operator /=(const Modint &oth) noexcept { x *= oth.inv(); x %= Mod; return *this; } constexpr Modint operator +(const Modint &oth) const noexcept { return Modint(x) += oth; } constexpr Modint operator -(const Modint &oth) const noexcept { return Modint(x) -= oth; } constexpr Modint operator *(const Modint &oth) const noexcept { return Modint(x) *= oth; } constexpr Modint operator /(const Modint &oth) const noexcept { return Modint(x) /= oth; } constexpr Modint operator -() const noexcept { return Modint((x != 0) * (Mod - x)); } template constexpr typename std::enable_if::value, const Modint&>::type operator =(T t) noexcept { (*this) = Modint(std::forward(t)); return *this; } constexpr Modint inv() const noexcept { return ::math::pow(*this, Mod - 2); } constexpr ll mod() const noexcept { return Mod; } private: ll x; }; } const ll mod = 1e9 + 7; using mint = math::Modint; struct Solver { using matrix = vvec; const ll m, k; matrix id_m; Solver(ll m, ll k) : m(m), k(k) { id_m = init_mat(); for (int i = 0; i < m; i++) id_m[i][i] = 1; } matrix init_mat() { return make_v(0, m, m); } matrix mul(const matrix &a, const matrix &b) { matrix ret = init_mat(); for (ll i = 0; i < m; i++) for (ll j = 0; j < m; j++) for (ll k = 0; k < m; k++) ret[i][k] += a[i][j] * b[j][k]; return ret; } matrix pow_mat(const matrix &m, ll k) { matrix ret = id_m, cur = m; while (k) { if (k & 1) ret = mul(ret, cur); cur = mul(cur, cur); k /= 2; } return ret; } mint solve() { matrix mat = init_mat(); for (ll i = 0; i < m; i++) for (ll j = 0; j < m; j++) { ll nxt1 = (i + j) % m; ll nxt2 = (i * j) % m; mat[nxt1][i] += 1; mat[nxt2][i] += 1; } auto powm = pow_mat(mat, k); return powm.front().front(); } }; int main() { ll m, k; std::cin >> m >> k; Solver solver(m, k); std::cout << solver.solve().value() << "\n"; return 0; }