N, p = map(int, input().split()) a = [0, 1] mod = 10**9+7 for i in range(2, N): a.append((p*a[-1]+a[-2])%mod) s = 0 res = 0 for i in range(N): s += a[i] s %= mod res += s*a[i] res %= mod print(res)