Domino rectangles

You have 4n identical 3-3 domino pieces, and you must cover with them a
3 × 3n rectangle. As you can see in the picture below, the positions
(2, 2), (2, 5), …, (2, 3n − 1) of the rectangle must be left empty.
Depending on n, how many different rectangles are possible?

For instance, these are the two only possible rectangles for n = 1, two
of the six possible rectangles for n = 2, and a possible rectangle for
n = 7:

[image]

Input

Input consists of several cases, each with two integer numbers n and m.
You can assume 0 ≤ n ≤ 10¹² and 2 ≤ m ≤ 10⁶.

Output

For every case, print the number of 3 × 3n rectangles modulo m.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T12:11:09.468Z

© Jutge.org, 2006–2026.
https://jutge.org
