#include #include using namespace std; using ll = long long; void ins() {} templatevoid ins(T& v,Rest&... rest){cin>>v;ins(rest...);} #define rep(i,n) for(int i=0,_i=(n);i<_i;++i) template struct MInt { long long val; constexpr MInt(long long val = 0) : val(val % MOD) { if (val < 0) val += MOD; } MInt operator+(const MInt& n) const { return MInt(val) += n; } MInt operator*(const MInt& n) const { return MInt(val) *= n; } MInt& operator+=(const MInt& n) { val = (val + n.val) % MOD; return *this; } MInt& operator*=(const MInt& n) { val = (val * n.val) % MOD; return *this; } friend ostream& operator<<(ostream& os, const MInt& n) { os<; template struct Matrix { int width, height; vector> v; Matrix(int w) : width(w), height(w), v(w, vector(w, 0)) {}; Matrix(int w, int h) : width(w), height(h), v(w, vector(h, 0)) {}; Matrix operator+(const Matrix& n) const { Matrix ans = v; return ans += n; } Matrix operator*(const Matrix& n) const { Matrix ans = v; return ans *= n; } Matrix& operator+=(const Matrix& rhs) { for (int i = 0; i < height; ++i) for (int j = 0; j < width; ++j) v[i][j] += rhs.v[i][j]; return *this; } Matrix& operator*=(const Matrix& rhs) { Matrix ans(height, rhs.width); for (int i = 0; i < height; ++i) for (int j = 0; j < rhs.width; ++j) for (int k = 0; k < width; ++k) ans[i][j] += v[i][k] * rhs.v[k][j]; width = rhs.width; v = ans.v; return *this; } Matrix pow(ll N) { Matrix pow = *this, ans(width); for (int i = 0; i < width; ++i) ans[i][i] = 1; for (ll p = N; p > 0; p >>= 1) { if (p & 1) ans *= pow; pow *= pow; } return ans; } vector& operator[](int i) { return v[i]; } }; int main() { int M, K; ins(M, K); Matrix m(M); rep(i, M) rep(j, M) { m[i][(i + j)%M] += 1; m[i][(i * j)%M] += 1; } m = m.pow(K); cout << m[0][0] << endl; return 0; }