N, p = map(int, input().split()) a = [0, 1] mod = 10**9+7 if N < 3: res = 0 for i in range(N): for j in range(i+1): res += a[i]*a[j] print(res) else: ss = 1 sm = 1 for i in range(2, N): n = p*a[-1]+a[-2] n %= mod ss += n**2 ss %= mod sm += n sm %= mod a.append(n) print(((sm**2-ss)//2+ss)%mod)