n, p = map(int, input().split()) if n == 1: print(0) else: res = 0 fibs = [0]*(2*n) fibs[0] = 0 fibs[1] = 1 for i in range(2*n): fibs[i] = fibs[i - 1] * p + fibs[i - 2] lastFib = fibs[n - 1]; for i in range(n): mod = (i + n) % 4; if mod == 1 or mod == 2: res += lastFib * fibs[i] res %= 1000000007 for i in range(2*n): mod = i % 4 if mod == 1 or mod == 2: res += (Min(i, (n - 1) * 2 - i) + 1) / 2 * fibs[i] res %= 1000000007 print(res)