atcoder#ARC157C. [ARC157C] YY Square

[ARC157C] YY Square

Score : 600600 points

Problem Statement

There is a grid with HH rows and WW columns where each square has one of the characters X and Y written on it. Let (i,j)(i, j) denote the square at the ii-th row from the top and jj-th column from the left. The characters on the grid are given as HH strings S1,S2,,SHS_1, S_2, \dots, S_H: the jj-th character of SiS_i is the character on square (i,j)(i, j).

For a path PP from square (1,1)(1, 1) to square (H,W)(H, W) obtained by repeatedly moving down or right to the adjacent square, the score is defined as follows.

  • Let str(P)\mathrm{str}(P) be the string of length (H+W1)(H + W - 1) obtained by concatenating the characters on the squares visited by PP in the order they are visited.
  • The score of PP is the square of the number of pairs of consecutive Ys in str(P)\mathrm{str}(P).

There are (H+W2H1)\displaystyle\binom{H + W - 2}{H - 1} such paths. Find the sum of the scores over all those paths, modulo 998244353998244353.

What is $\binom{N}{K}$? \displaystyle\binom{N}{K}$$$$K$$$$N

Constraints

  • 1H20001 \leq H \leq 2000
  • 1W20001 \leq W \leq 2000
  • Si (1iH)S_i \ (1 \leq i \leq H) is a string of length WW consisting of X and Y.

Input

The input is given from Standard Input in the following format:

HH WW

S1S_1

S2S_2

\vdots

SHS_H

Output

Print the sum of the scores over all possible paths, modulo 998244353998244353.

2 2
YY
XY
4

There are two possible paths PP: (1,1)(1,2)(2,2)(1, 1) \to (1, 2) \to (2, 2) and (1,1)(2,1)(2,2)(1, 1) \to (2, 1) \to (2, 2).

  • For (1,1)(1,2)(2,2)(1, 1) \to (1, 2) \to (2, 2), we have str(P)=\mathrm{str}(P) = {}YYY, with two pairs of consecutive Ys at positions 1,21, 2 and 2,32, 3, so the score is 22=42^2 = 4.
  • For (1,1)(2,1)(2,2)(1, 1) \to (2, 1) \to (2, 2), we have str(P)=\mathrm{str}(P) = {}YXY, with no pairs of consecutive Ys , so the score is 02=00^2 = 0.

Thus, the sought sum is 4+0=44 + 0 = 4.

2 2
XY
YY
2

For either of the two possible paths PP, we have str(P)=\mathrm{str}(P) = {}XYY, for a score of 12=11^2 = 1.

10 20
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
423787835

Print the sum of the scores modulo 998244353998244353.