n, p = map(int, input().split()) res = int(0) if n != 1: fibs = [0]*(2*n) fibs[0] = int(0) fibs[1] = int(1) for i in range(2, 2*n): fibs[i] = fibs[i - 1] * p + fibs[i - 2] fibs[i] %= 1000000007 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)