#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < int(n); i++) using namespace std; long long MOD = 1000000007; long long INF = 1000000000000000; //10^15 typedef long long ll; typedef unsigned long long ull; ll powMod(ll x, ll n, ll mod) { if (n == 0) return 1; ll t = powMod(x, n/2, mod); t = t * t % mod; if (n & 1) return t * x % mod; return t; } ll gcd(ll a, ll b) { if (a == 0 || b == 0) return a + b; if (b > a) return gcd(b, a); return gcd(b, a % b); } void MatrixPro(ll *g, ll *h, ll *m, int size) { //g * h = m rep(i, size) rep(j, size) { m[size * i + j] = 0; rep(k, size) { m[size * i + j] += g[size * i + k] * h[size * k + j] % MOD; m[size * i + j] %= MOD; } } return; } //g^n = h, size <= 100 void MatrixExpo(ll *g, ll *h, int size, ll n) { if (n == 0) { rep(i, size) rep(j, size) { if (i == j) h[size * i + j] = 1; else h[size * i + j] = 0; } return; } ll a[10010]; MatrixExpo(g, a, size, n / 2); ll b[10010]; MatrixPro(a, a, b, size); if (!(n & 1)) { rep(i, size) rep(j, size) { h[size * i + j] = b[size * i + j]; } return; } else { MatrixPro(b, g, h, size); return; } } int main(void) { ll m, k; cin >> m >> k; ll x[3000] = {}; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { int t = i*j%m; x[t*m+i]++; t = (i+j)%m; x[t*m+i]++; } } //rep(i, m*m) cout << x[i] << endl; ll y[3000] = {}; MatrixExpo(x, y, m, k); cout << y[0] << endl; }