def mat_mul(A, B): C = [[0]*len(B[0]) for _ in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(A[0])): C[i][j] += A[i][k]*B[k][j] C[i][j] %= MOD return C N, p = map(int, input().split()) MOD = 10**9+7 A = [[p, 1], [1, 0]] arr = [[1], [0]] acc = 1 ans = 1 for _ in range(N-2): arr = mat_mul(A, arr) ai = arr[0][0] acc += ai acc %= MOD ans += acc*ai ans %= MOD print(ans)