#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rrep(i,m,n) for(int (i)=(int)(m);(i)>=(int)(n);(i)--) #define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++) #define REP(i,n) rep(i,0,n) #define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(),(hoge).end() constexpr ll MOD = 1000000007; typedef vector Array; typedef vector Matrix; Matrix mIdentity(ll n) { Matrix A(n, Array(n)); for (int i = 0; i < n; ++i) A[i][i] = 1; return A; } Matrix mMul(const Matrix& A, const Matrix& B, ll mod) { Matrix C(A.size(), Array(B[0].size())); for (int i = 0; i < C.size(); ++i) for (int k = 0; k < A[i].size(); ++k) for (int j = 0; j < C[i].size(); ++j) (C[i][j] += (A[i][k] % mod) * (B[k][j] % mod)) %= mod; return C; } // O( n^3 log e ) Matrix mPow(const Matrix& A, ll e, ll mod) { return e == 0 ? mIdentity(A.size()) : e % 2 == 0 ? mPow(mMul(A, A, mod), e / 2, mod) : mMul(A, mPow(A, e - 1, mod), mod); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll m, k; cin >> m >> k; Matrix ini(m, Array(m, 0)); ini[0][0] = 1; Matrix trans(m, Array(m, 1)); REP(i, m) { REP(j, m) { ll h = i; ll w = i * j % m; trans[h][w]++; } } Matrix ans = mMul(ini, mPow(trans, k, MOD), MOD); cout << ans[0][0] << endl; return 0; }