import numpy as np #誤差死する n,k = map(int,input().split()) matrix = np.zeros((k**3, k**3), dtype = int) for i in range(k**3): val = i aCount = val % k val = val // k abCount = val % k val = val // k abcCount = val % k #aが追加された時 newACount = (aCount + 1) % k newABCount = abCount newABCCount = abcCount matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1 #bが追加された時 newACount = aCount newABCount = (abCount + aCount) % k newABCCount = abcCount matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1 #cが追加された時 newACount = aCount newABCount = abCount newABCCount = (abcCount + abCount) % k matrix[i, (newABCCount * k + newABCount) * k + newACount] += 1 res = np.zeros((1, k**3), dtype = int) res[0, 0] = 1 mod = 1000000007 while n > 0: if n % 2 == 1: res = np.mod(np.matmul(res, matrix), mod) matrix = np.mod((np.matmul(matrix, matrix)),mod) n = n // 2 numRes = 0 for i in range(k**2): numRes += res[0, i] print(numRes % mod)