P, K = gets.split.map(&:to_i) MOD = 10 ** 9 + 7 dp = Array.new(K + 2) { Array.new(2, 0) } dp[0][0] = 1 K.times do |i| # zero * num dp[i + 1][0] += P * dp[i][0] # zero + non zero dp[i + 1][1] += (P - 1) * dp[i][0] # (non zero + zero) dp[i + 1][0] += dp[i][0] # non zero * zero dp[i + 1][0] += dp[i][1] # (non zero + non zero) % K == 0 dp[i + 1][0] += dp[i][1] # (non zero + num) % K != 0 dp[i + 1][1] += (P - 1) * dp[i][1] # (non zero * num) % K != 0 dp[i + 1][1] += (P - 1) * dp[i][1] dp[i + 1][0] %= MOD dp[i + 1][1] %= MOD end puts dp[K][0]