from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline N,p = map(int,input().split()) mod = 10**9 + 7 if N==1: print(0) exit() a = [0]*(N+1) a[2]=1 ans = 0 S = 0 ans = 1 S = 1 for i in range(3,N+1): a[i]=(p*a[i-1]+a[i-2])%mod S += a[i] ans += S*a[i] ans %= mod S %= mod print(ans)