You have a grid with $H$ rows and $W$ columns. Each cell contains one of the following 11 characters: an addition operator +
, a multiplication operator *
, or a digit between $1$ and $9$.
There are paths from the top-left cell to the bottom-right cell by moving right or down $H+W-2$ times. Let us define the value of a path by the evaluation result of the mathematical expression you can obtain by concatenating all the characters contained in the cells on the path in order. Your task is to compute the sum of values of any possible paths. Since the sum can be large, find it modulo $M$.
It is guaranteed the top-left cell and the bottom-right cell contain digits. Moreover, if two cells share an edge, at least one of them contains a digit. In other words, each expression you can obtain from a path is mathematically valid.
The input consists of a single test case in the format below.
$H$ $W$ $M$ $a_{1,1}$ $\cdots$ $a_{1,W}$ $\ldots$ $a_{H,1}$ $\cdots$ $a_{H,W}$
The first line consists of three integers $H$, $W$ and $M$ ($1 \le H,W \le 2 000$, $2 \le M \le 10^{9}$).The following $H$ lines represent the characters on the grid. $a_{i,j}$ represents the character contained in the cell at the $i$-th row and $j$-th column. Each $a_{i,j}$ is either +
, *
, or a digit between $1$ and $9$. $a_{1,1}$ and $a_{H,W}$ are both digits. If two cells share an edge, at least one of them contain a digit.
Print the sum of values of all possible paths modulo $M$.
Input | Output |
---|---|
2 3 1000 3*1 +27 | 162 |
4 4 3000000 24+7 *23* 9+48 *123 | 2159570 |