import java.util.*; public class Main { static final int MOD = 1000000007; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int kk = sc.nextInt(); long[][][] dp = new long[m][m][31]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { dp[i][(i + j) % m][0]++; dp[i][i * j % m][0]++; } } for (int i = 1; i < 31; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { for (int l = 0; l < m; l++) { dp[j][l][i] += dp[j][k][i - 1] * dp[k][l][i - 1] % MOD; dp[j][l][i] %= MOD; } } } } long[] base = new long[m]; base[0] = 1; for (int i = 30; i >= 0 && kk > 0; i--) { if (kk < (1 << i)) { continue; } kk -= (1 << i); long[] next = new long[m]; for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { next[k] += base[j] * dp[j][k][i] % MOD; next[k] %= MOD; } } base = next; } System.out.println(base[0]); } }