Colored Brackets

Count valid bracket strings of 2N brackets over K colors that equal their own reversal, modulo 1e9+7.

Hard9CombinatoricsMathDynamic programmingNumber theoryNo attempts yetTime limit2sMemory limit512 MB

Problem

A valid bracket string is defined by the following rules.

  • The empty string is a valid bracket string.
  • If SS is a valid bracket string, then (S)(S) is a valid bracket string. Putting an opening bracket in front of a valid bracket string and a closing bracket behind it keeps it valid.
  • If SS and TT are valid bracket strings, then STST is a valid bracket string. Concatenating valid bracket strings keeps them valid.

Someone likes bracket strings very much. Using a single kind of bracket got dull, so he decided to paint the brackets with KK colors that can be told apart. An opening bracket of color ii pairs only with a closing bracket of color ii. For example, if K=3K = 3 and the colors are red, green and blue, the second rule above grows into this.

  • If SS is a valid bracket string, then (S)(S) wrapped in red brackets, (S)(S) wrapped in green brackets and (S)(S) wrapped in blue brackets are all valid bracket strings. The opening bracket and the closing bracket of one pair always have the same color.

For a larger KK, add more distinguishable colors and extend the definition the same way.

Reversing a string means writing it down the way it looks in a mirror. The order of the characters is reversed, and each bracket becomes the bracket of the opposite direction in the same color. The colors stay where they are. With a single color, reversing (())() gives ()(()). That string differs from its reversal, so it must not be counted. ()(())() stays ()(())() after reversing, so it must be counted.

Count the valid bracket strings built from 2N2N brackets in KK colors that are equal to their own reversal.

Input

The first line contains two integers NN, the number that fixes how many brackets are used, and KK, the number of colors, separated by a space. (1N1061 \le N \le 10^6, 1K1061 \le K \le 10^6)

Output

Print how many valid bracket strings built from 2N2N brackets in KK colors are equal to their own reversal. This number can grow very large, so print it modulo 1,000,000,007.

Hint

For N=2N = 2 and K=2K = 2 there are 6 strings that satisfy the condition. Writing the two colors as 1 and 2 in subscripts, they are:

(1(1)1)1(_1(_1)_1)_1, (1(2)2)1(_1(_2)_2)_1, (2(1)1)2(_2(_1)_1)_2, (2(2)2)2(_2(_2)_2)_2, (1)1(1)1(_1)_1(_1)_1, (2)2(2)2(_2)_2(_2)_2

The six cases