#include #include #include int ri() { int n; scanf("%d", &n); return n; } #define MOD 1000000007 int main() { int m = ri(); int k = ri(); int vec[m] = { 0 }; vec[0] = 1; int mat[m][m]; memset(mat, 0, sizeof(mat)); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { mat[i][(i + j) % m]++; mat[i][(int64_t) i * j % m]++; } } for (; k; k >>= 1) { if (k & 1) { int next[m] = { 0 }; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) next[j] = (next[j] + (int64_t) vec[i] * mat[i][j]) % MOD; memcpy(vec, next, sizeof(next)); } int next[m][m]; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) { uint64_t sum = 0; for (int k = 0; k < m / 3; k++) sum += (int64_t) mat[i][k] * mat[k][j]; sum %= MOD; for (int k = m / 3; k < m * 2 / 3; k++) sum += (int64_t) mat[i][k] * mat[k][j]; sum %= MOD; for (int k = m * 2 / 3; k < m; k++) sum += (int64_t) mat[i][k] * mat[k][j]; next[i][j] = sum % MOD; } memcpy(mat, next, sizeof(next)); } printf("%d\n", vec[0]); return 0; }