import sys import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines P, K = map(int, read().split()) MOD = 10 ** 9 + 7 def mat_mul(A, B): mask = (1 << 15) - 1 A1 = A >> 15 A2 = A & mask B1 = B >> 15 B2 = B & mask X = np.dot(A1, B1) % MOD Y = np.dot(A2, B2) % MOD Z = (np.dot(A1 + A2, B1 + B2) - X - Y) % MOD return ((X << 30) + (Z << 15) + Y) % MOD def power(A, n): if n == 1: return A B = power(A, n // 2) B = mat_mul(B, B) if n & 1: B = mat_mul(A, B) return B A = np.array([[P+1, 2], [P-1,2*P-2]], np.int64) print(power(A, K)[0,0])