#agc055d. [agc055_d]ABC Ultimatum
[agc055_d]ABC Ultimatum
Problem Statement
Let's call a string of length , containing exactly letters A
, letters B
, and letters C
, good, if it can be split into (not necessarily contiguous) subsequences of length , so that the letters from each subsequence form ABC
, BCA
, or CAB
.
You are given a string of length , consisting of characters A
, B
, C
, and ?
. Count the number of ways to replace each ?
with one of A
, B
, and C
, so that the resulting string is good. As this number can be very large, output it modulo .
Constraints
- .
- is a string of length , consisting of
A
,B
,C
, and?
.
Input
Input is given from Standard Input in the following format:
Output
Print the answer modulo .
Sample Input 1
1
???
Sample Output 1
3
There are good strings you can obtain: ABC
, BCA
, and CAB
.
Sample Input 2
2
AA????
Sample Output 2
2
There are good strings you can obtain: AABBCC
and AABCBC
.
Sample Input 3
3
?A?A?A?A?
Sample Output 3
0
It's impossible to obtain a good string, as there are already A
's.
Sample Input 4
9
?????????A??B??C???????????
Sample Output 4
331653164