MOD = 10**9 + 7

n, p = map(int, input().split())

if n == 1:
    print(0)
else:
    a_prev_prev = 0  # a1
    a_prev = 1       # a2
    sum_s_prev = 1   # sum of a1 + a2 = 0 + 1 = 1
    total_sum = 1    # initial contribution from i=2: 1*1=1

    if n == 2:
        print(total_sum % MOD)
    else:
        for i in range(3, n + 1):
            current_a = (p * a_prev + a_prev_prev) % MOD
            sum_s_current = (sum_s_prev + current_a) % MOD
            contribution = (current_a * sum_s_current) % MOD
            total_sum = (total_sum + contribution) % MOD
            # Update variables for next iteration
            a_prev_prev, a_prev = a_prev, current_a
            sum_s_prev = sum_s_current
        print(total_sum % MOD)