luogu#P5023. [NOIP 2018 提高组] 填数游戏
[NOIP 2018 提高组] 填数游戏
Background
NOIP 2018 Senior D2T2.
Problem Description
Xiao D really likes playing games. One day, he was playing a number filling game.
The board of this game is an rectangular grid. The player needs to fill each cell in the grid with a number (either or ), and the filling must satisfy some constraints.
We describe these constraints in detail below.
For convenience, we first give some definitions:
-
We use the row and column coordinates of each cell to represent a cell, i.e., (row, column). Note: both row and column coordinates start from .
-
A valid path : A path is valid if and only if:
- The path starts from the top-left cell of the grid and ends at the bottom-right cell ;
- Along this path, each move can only go from the current cell to the adjacent cell on its right, or from the current cell to the adjacent cell below it.
For example, in the rectangle below, only two paths are valid: : and : .

For a valid path , we can represent it with a string of length , which contains only the characters or . The -th character records how the -th step of path moves. means moving to the adjacent cell on the right of the current cell, and means moving to the adjacent cell below the current cell. For example, for path in the figure above, ; for the other path , .
Also, if we concatenate, in order, the numbers filled in each cell visited by a valid path , we obtain a string of length , denoted by . For example, if we fill in cells and , and fill in cells and (as the red numbers in the figure), then for path we get , and for path we get .
The game requires Xiao D to find a way to fill and such that for any two paths , , if , then it must hold that . We say that string is smaller than string if and only if is lexicographically smaller than . The definition of lexicographical order is given in detail in Problem 1. However, finding just one method does not satisfy Xiao D’s curiosity. Xiao D wants to know how many different ways the game can be played, that is, how many filling methods satisfy the requirement.
Xiao D is not strong enough, so he hopes you can help solve this problem: how many ways of filling and can satisfy the requirement? Since the answer may be very large, you need to output the answer modulo .
Input Format
The input consists of one line containing two positive integers , separated by a space, representing the size of the rectangle. Here is the number of rows of the grid, and is the number of columns of the grid.
Output Format
Output one line containing a positive integer, representing how many ways of filling and can satisfy the requirement. Note: output the result modulo .
2 2
12
3 3
112
5 5
7136
Hint
Sample Explanation

Constraints
| Test Point ID | ||
|---|---|---|
Translated by ChatGPT 5