atcoder#AGC055D. [AGC055D] ABC Ultimatum
[AGC055D] ABC Ultimatum
Score : points
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 .
1
???
3
There are good strings you can obtain: ABC
, BCA
, and CAB
.
2
AA????
2
There are good strings you can obtain: AABBCC
and AABCBC
.
3
?A?A?A?A?
0
It's impossible to obtain a good string, as there are already A
's.
9
?????????A??B??C???????????
331653164