#R119F. [ARC119F] AtCoder Express 3
[ARC119F] AtCoder Express 3
Score: points
Problem Statement
There are stations along the AtCoder Line, numbered through . Formerly, it only had Local Trains, which run between Stations and in one minute in either direction for each .
One day, however, the railway company broke into two groups, called Ko-soku (light speed) and Jun-kyu (semi express). They decided that each station other than Stations and should be administered by one of the groups. The group that administers Station is represented by a character : A means that Ko-soku administers the station, B means Jun-kyu administers the station, and ? means it is undecided. Since Stations and are so important, both groups will administer them.
Now, Ko-soku and Jun-kyu have decided to run two new types of trains, in addition to the local trains.
Ko-soku Trains: Let Stations be the stations administered by Ko-soku in ascending order. These trains run between Stations and in one minute in either direction for each .
Jun-kyu Trains: Let Stations be the stations administered by Jun-kyu in ascending order. These trains run between Stations and in one minute in either direction for each .
There are ways in which these trains run, where is the number of
?s. Among them, how many enables us to go from Station to Station in no more than minutes' ride? Find this count modulo .
Constraints
- and are integers.
- Each of is
A,B, or?.
Input
Input is given from Standard Input in the following format:
Output
Print the count modulo .
8 3
A??AB?B
4
Here, there are possible ways in which the trains run. Among them, the following four enable us to go from Station to Station in no more than minutes' ride.
- If Ko-soku administers Stations , we can go Station , as shown at #1 in the figure below.
- If Ko-soku administers Stations and Jun-kyu administers Station , we can go Station , as shown at #2 in the figure below.
- If Ko-soku administers Station and Jun-kyu administers Stations , we can go Station , as shown at #4 in the figure below.
- If Jun-kyu administers Stations , we can go Station , as shown at #8 in the figure below.
Therefore, the answer is . The figure below shows all the possible ways, where red stations and railways are administered only by Ko-soku, and blue stations and railways are administered only by Jun-kyu.

11 6
???B??A???
256
Here, all of the ways enable us to go from Station to Station in no more than minutes' ride.
16 5
?A?B?A?B?A?B?A?
10
ways shown in the figure below enable us to go from Station to Station in no more than minutes' ride.

119 15
????????A?????????????????????????BA????????????????????????AB???????A???????????B?????????????????????????????A??????
313346281
There are desirable ways. Print this count modulo , that is, .