atcoder#ARC120B. [ARC120B] Uniformly Distributed

[ARC120B] Uniformly Distributed

Score : 400400 points

Problem Statement

We have a grid with HH rows and WW columns. Let (i,j)(i, j) denote the square at the ii-th row and jj-th column. You are given HH strings S1,S2,S3,,SHS_1, S_2, S_3, \dots, S_H that describe the square, as follows:

  • if the jj-th character of SiS_i is R, (i,j)(i, j) is painted red;
  • if the jj-th character of SiS_i is B, (i,j)(i, j) is painted blue;
  • if the jj-th character of SiS_i is ., (i,j)(i, j) is unpainted.

Let kk be the number of unpainted squares. Among the 2k2^k ways to paint each of those squares red or blue, how many satisfy the following condition?

  • The number of red squares visited while getting from (1,1)(1, 1) to (H,W)(H, W) by repeatedly moving one square right or down, including (1,1)(1, 1) and (H,W)(H, W), is always the same regardless of the path taken.

Since the count may be enormous, print it modulo 998244353998244353.

Constraints

  • 2H5002 \le H \le 500
  • 2W5002 \le W \le 500
  • SiS_i is a string of length WW consisting of R, B, and ..

Input

Input is given from Standard Input in the following format:

HH WW

S1S_1

S2S_2

S3S_3

\hspace{3pt} \vdots

SHS_H

Output

Print the count modulo 998244353998244353.

2 2
B.
.R
2

There are two ways to get from (1,1)(1, 1) to (2,2)(2, 2) by repeatedly moving right or down, as follows:

  • (1,1)(1,2)(2,2)(1, 1) \rightarrow (1, 2) \rightarrow (2, 2)
  • (1,1)(2,1)(2,2)(1, 1) \rightarrow (2, 1) \rightarrow (2, 2)

If we paint (1,2)(1, 2) and (2,1)(2, 1) in different colors, the above two paths will contain different numbers of red squares, violating the condition. On the other hand, if we paint (1,2)(1, 2) and (2,1)(2, 1) in the same color, the two paths will contain the same numbers of red squares, satisfying the condition. Thus, there are two ways to satisfy the condition.

3 3
R.R
BBR
...
0

There may be no way to satisfy the condition.

2 2
BB
BB
1

There is no unpainted square, and the current grid satisfies the condition, so the answer is 11.