def main(): import sys input = sys.stdin.readline N, p = map(int, input().split()) mod = 1000000007 A = [0] * N A[1] = 1 S = [0] * N S[1] = 1 for i in range(2, N): A[i] = ((p*A[i-1])%mod + A[i-2])%mod S[i] = S[i-1] + A[i] ans = 0 for i in range(N): ans = (ans + (A[i] * S[i])%mod)%mod print(ans) if __name__ == '__main__': main()