using System; using System.Collections.Generic; using System.Linq; using System.IO; using SB = System.Text.StringBuilder; //using System.Threading.Tasks; //using System.Text.RegularExpressions; //using System.Globalization; //using System.Diagnostics; using static System.Console; using System.Numerics; using static System.Math; using pair = Pair; class Program { static void Main() { //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false }); new Program().solve(); Out.Flush(); } readonly Scanner cin = new Scanner(); readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑ readonly int mod = 1000000007; readonly int dom = 998244353; bool chmax(ref T a, T b) where T : IComparable { if (a.CompareTo(b) < 0) { a = b; return true; } return false; } bool chmin(ref T a, T b) where T : IComparable { if (b.CompareTo(a) < 0) { a = b; return true; } return false; } int id(int a, int ab, int abc) { return a + ab * K + abc * K * K; } int K; void solve() { int N = cin.nextint; K = cin.nextint; int L = K * K * K; var M = new ModInt[L][]; for (int i = 0; i < L; i++) { M[i] = new ModInt[L]; } for (int i = 0; i < K; i++) { for (int j = 0; j < K; j++) { for (int k = 0; k < K; k++) { //aが追加される int toi = (i + 1) % K; M[id(toi, j, k)][id(i, j, k)] += 1; //bが追加される int toj = (j + i) % K; M[id(i, toj, k)][id(i, j, k)] += 1; //cが追加される int tok = (k + j) % K; M[id(i, j, tok)][id(i, j, k)] += 1; } } } M = matpow(M, N); ModInt ans = 0; for (int i = 0; i < K; i++) { for (int j = 0; j < K; j++) { ans += M[id(i, j, 0)][0]; } } WriteLine(ans); } ModInt[][] matpow(ModInt[][] m, long a) { if (a == 0) { int N = m.Length; ModInt[][] ret = new ModInt[N][]; for (int i = 0; i < N; i++) { ret[i] = new ModInt[N]; ret[i][i] = 1; } return ret; } if (a % 2 == 0) { ModInt[][] ret = matpow(m, a / 2); return matmul(ret, ret); } else { return matmul(m, matpow(m, a - 1)); } } ModInt[][] matmul(ModInt[][] ma, ModInt[][] mb) { int p = ma.Length; int q = mb[0].Length; int r = ma[0].Length; ModInt[][] ret = new ModInt[p][]; for (int i = 0; i < p; i++) { ret[i] = new ModInt[q]; for (int j = 0; j < q; j++) { for (int k = 0; k < r; k++) { ret[i][j] += ma[i][k] * mb[k][j]; } } } return ret; } } /// /// [0,) までの値を取るような数 /// /// camypaper struct ModInt { /// /// 剰余を取る値. /// public const long Mod = (int)998244353; /// /// 実際の数値. /// public long num; /// /// 値が であるようなインスタンスを構築します. /// /// インスタンスが持つ値 /// パフォーマンスの問題上,コンストラクタ内では剰余を取りません.そのため, ∈ [0,) を満たすような を渡してください.このコンストラクタは O(1) で実行されます. public ModInt(long n) { num = n; } /// /// このインスタンスの数値を文字列に変換します. /// /// [0,) の範囲内の整数を 10 進表記したもの. public override string ToString() { return num.ToString(); } public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; } public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; } public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); } public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); } /// /// 与えられた 2 つの数値からべき剰余を計算します. /// /// べき乗の底 /// べき指数 /// 繰り返し二乗法により O(N log N) で実行されます. public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); } /// /// 与えられた 2 つの数値からべき剰余を計算します. /// /// べき乗の底 /// べき指数 /// 繰り返し二乗法により O(N log N) で実行されます. public static ModInt Pow(long v, long k) { long ret = 1; for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod) if ((k & 1) == 1) ret = ret * v % Mod; return new ModInt(ret); } /// /// 与えられた数の逆元を計算します. /// /// 逆元を取る対象となる数 /// 逆元となるような値 /// 法が素数であることを仮定して,フェルマーの小定理に従って逆元を O(log N) で計算します. public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); } } class BinomialCoefficient { public ModInt[] fact, ifact; /// /// 未満でお願いします。 /// /// public BinomialCoefficient(ModInt _n) { int n = (int)_n.num; fact = new ModInt[n + 1]; ifact = new ModInt[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = ModInt.Inverse(fact[n]); for (int i = n - 1; i >= 0; i--) ifact[i] = ifact[i + 1] * (i + 1); ifact[0] = ifact[1]; } public ModInt this[int n, int r] { get { if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0; return fact[n] * ifact[n - r] * ifact[r]; } } public ModInt RepeatedCombination(int n, int k) { if (k == 0) return 1; return this[n + k - 1, k]; } } static class Ex { public static void join(this IEnumerable values, string sep = " ") => WriteLine(string.Join(sep, values)); public static string concat(this IEnumerable values) => string.Concat(values); public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); } public static int lower_bound(this IList arr, T val) where T : IComparable { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) < 0) low = mid + 1; else high = mid; } return low; } public static int upper_bound(this IList arr, T val) where T : IComparable { int low = 0, high = arr.Count; int mid; while (low < high) { mid = ((high - low) >> 1) + low; if (arr[mid].CompareTo(val) <= 0) low = mid + 1; else high = mid; } return low; } } class Pair : IComparable> where T : IComparable where U : IComparable { public T f; public U s; public Pair(T f, U s) { this.f = f; this.s = s; } public int CompareTo(Pair a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s); public override string ToString() => $"{f} {s}"; } class Scanner { string[] s; int i; readonly char[] cs = new char[] { ' ' }; public Scanner() { s = new string[0]; i = 0; } public string[] scan => ReadLine().Split(); public int[] scanint => Array.ConvertAll(scan, int.Parse); public long[] scanlong => Array.ConvertAll(scan, long.Parse); public double[] scandouble => Array.ConvertAll(scan, double.Parse); public string next { get { if (i < s.Length) return s[i++]; string st = ReadLine(); while (st == "") st = ReadLine(); s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries); i = 0; return next; } } public int nextint => int.Parse(next); public long nextlong => long.Parse(next); public double nextdouble => double.Parse(next); }