Under construction

Still being built. Things may change

Mosaic

Time limit1sMemory limit128 MB

Summary
Count the tilings of an N x M grid with 2 x 2 squares and L-trominoes, modulo 1,000,000.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

An architect wants to decorate a building with a long, thin mosaic. The mosaic occupies a rectangular strip that is NN inches tall and MM inches wide. Think of the strip as an N×MN \times M grid of unit cells, where each cell is one inch by one inch.

He has two kinds of tiles, and each tile fits inside a 2×22 \times 2 block of cells:

The two kinds of tiles

  • A square tile that covers all four cells of a 2×22 \times 2 block.
  • An L-shaped tile (an L-tromino) that covers exactly three cells of a 2×22 \times 2 block, leaving one corner cell empty. This tile may be rotated into any of its four orientations.

He wants to cover the whole strip so that every cell is covered exactly once, with no gaps and no overlapping tiles. He wonders how many different patterns he can make.

Two mosaics are considered the same only when they use the same kinds of tiles in exactly the same positions. If a rotation or reflection of a pattern places tiles in different cells, it counts as a different pattern. For example, the four 4×164 \times 16 mosaics below are rotations or reflections of one another, yet the architect counts them as four different mosaics:

Four example mosaics

Input

The input contains several test cases. Each test case is a single line with two integers NN and MM (2≤N≤102 \le N \le 10, 2≤M≤5002 \le M \le 500): the height and the width of the strip, in inches. The input ends with a line containing two zeros; this terminating line is not a test case.

Output

For each test case, print one line containing a single integer: the number of distinct tilings of the N×MN \times M strip, taken modulo 106=1,000,00010^6 = 1{,}000{,}000. Print no extra whitespace, and print no blank lines between answers.

Examples3

  1. Example 1

    Input
    2 10
    4 16
    4 50
    0 0
    
    Expected output
    25
    366318
    574777
    
  2. Example 2

    Input
    2 2
    0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    2 3
    0 0
    
    Expected output
    2