Nice Prefixes

Time limit1sMemory limit128 MB

Summary
Count length-L strings over a K-letter alphabet where every prefix keeps all symbol counts within 2 of each other, modulo 1e9+7, with L up to 1e18.
Level

Hard9 of 10

Topics
Dynamic programming, Combinatorics, Math, Matrix
Solved
No attempts yet

Problem

Consider strings formed from an alphabet of size KK. For example, if K=4K = 4 the alphabet might be {a,b,c,d}\{a, b, c, d\}, and one such string is bbcacbbcac.

For a string SS, let count(S,k)\mathrm{count}(S, k) be the number of times the symbol kk occurs in SS. For example, count(bbcac,b)=2\mathrm{count}(bbcac, b) = 2 and count(bbcac,a)=1\mathrm{count}(bbcac, a) = 1.

A prefix of a string SS is any string obtained by deleting zero or more trailing characters of SS. For example, the prefixes of acbacb are the empty string, aa, acac, and acbacb.

A string SS has nice prefixes if for every prefix PP of SS and every two alphabet symbols k1k_1 and k2k_2, ∣count(P,k1)−count(P,k2)∣≤2|\mathrm{count}(P, k_1) - \mathrm{count}(P, k_2)| \le 2. For example, bbcacbbcac has nice prefixes, but abbbcabbbc does not, because count(abbb,b)=3\mathrm{count}(abbb, b) = 3 and count(abbb,c)=0\mathrm{count}(abbb, c) = 0.

Count the number of strings of length LL over an alphabet of size KK that have nice prefixes. This number can be large, so print it modulo 10000000071000000007.

Input

A single line with two integers LL and KK separated by a space, where 1≤L≤10181 \le L \le 10^{18} and 1≤K≤501 \le K \le 50.

Output

Print a single line with the number of length-LL strings over an alphabet of size KK that have nice prefixes, taken modulo 10000000071000000007.

Examples1

  1. Example 1

    Input
    4 2
    
    Expected output
    12