#arc157c. [arc157_c]YY Square
[arc157_c]YY Square
Problem Statement
There is a grid with rows and columns where each square has one of the characters X
and Y
written on it. Let denote the square at the -th row from the top and -th column from the left. The characters on the grid are given as strings : the -th character of is the character on square .
For a path from square to square obtained by repeatedly moving down or right to the adjacent square, the score is defined as follows.
- Let be the string of length obtained by concatenating the characters on the squares visited by in the order they are visited.
- The score of is the square of the number of pairs of consecutive
Y
s in .
There are such paths. Find the sum of the scores over all those paths, modulo .
What is ? is the binomial coefficient representing the number of ways to choose from distinct elements.
Constraints
- is a string of length consisting of
X
andY
.
Input
The input is given from Standard Input in the following format:
Output
Print the sum of the scores over all possible paths, modulo .
Sample Input 1
2 2
YY
XY
Sample Output 1
4
There are two possible paths : and .
- For , we have
YYY
, with two pairs of consecutiveY
s at positions and , so the score is . - For , we have
YXY
, with no pairs of consecutiveY
s , so the score is .
Thus, the sought sum is .
Sample Input 2
2 2
XY
YY
Sample Output 2
2
For either of the two possible paths , we have XYY
, for a score of .
Sample Input 3
10 20
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
Sample Output 3
423787835
Print the sum of the scores modulo .