class ModInteger attr_reader :fac, :inv, :finv, :mod MOD = 10 ** 9 + 7 def initialize(n, mod: MOD) @mod = mod @fac = [1, 1] @inv = [1, 1] @finv = [1, 1] (2..n).each do |i| @fac[i] = fac[i - 1] * i % mod @inv[i] = mod - inv[mod % i] * (mod / i) % mod @finv[i] = finv[i - 1] * inv[i] % mod end end def combination(n, k) return 0 if n < k return 0 if n < 0 || k < 0 fac[n] * (finv[k] * finv[n - k] % mod) % mod end def permutation(n, k = n) return 0 if n < k return 0 if n < 0 || k < 0 fac[n] * (finv[n - k] % mod) % mod end def repeated_combination(n, k) combination(n + k - 1, k) end end N, P = gets.split.map(&:to_i) MOD = 10 ** 9 + 7 mi = ModInteger.new(N + 1) if N == 1 puts 0 exit end a1 = 0 a2 = 1 ans = 1 sum = 1 (N - 2).times do |i| a3 = (P * a2 + a1) % MOD sum += a3 sum %= MOD ans += a3 * sum ans %= MOD a1, a2 = a2, a3 end puts ans