n,p = map(int,input().split()) mod = 10**9+7 ans = 0 cum = 0 fib = [0,1] for i in range(n): if i >= 2: fib.append((fib[-1]*p+fib[-2])%mod) cum += fib[i] cum %= mod ans += cum*fib[i] ans %= mod print(ans)