It's game night at Amplitude, which means a group of individuals are playing The Gang!
Actually, this summer, The Gang is not as popular. That doesn't stop there from being a problem that is tangentially related.
Nick came up with the following problem idea themed around The Gang and expected values: you are given a string that
only contains four characters:
A
, G
, N
, and ?
. Nick will independently and uniformly at random take each question mark and
replace it with one of A
, G
or N
. Compute the expected number of times GANG
appears as a subsequence in the string.
Expected values are terrifying to people at Amplitude, so he has modified the problem slightly. Instead of computing the expected value,
consider all possible strings that can be obtained after independently replacing each question mark with one of A
, G
, or N
. For
each such string, count the number of times GANG
appears as a subsequence, and sum these counts over all possible strings.
Because the number of ways that GANG
can appear as a subsequence can be very large, please keep track of the number of ways
modulo . It is highly recommended that you maintain a running count of the answer modulo
and not simply output
the final result modulo
.
The number of times GANG
appears as a subsequence in a string is equal to the number of unique
-tuples
where
,
,
,
, and
.
Constraints
Subtask 1 [1 point]
Subtask 2 [1 point]
Subtask 3 [1 point]
No additional constraints.
Input Specification
The first and only line of input contains a single string . It is guaranteed each character in
comes from
AGN?
.
Output Specification
Let be the sum of the total number of times
GANG
appears a subsequence over all strings that can be obtained from .
Output the remainder when is divided by
998244353
.
Sample Input 1
????
Sample Output 1
1
Sample Explanation 1
There is exactly one string that has GANG
appear as a subsequence - namely, GANG
.
This test case will be the first test case in the first subtask.
Sample Input 2
?????????????????
Sample Output 2
799755681
Sample Explanation 2
It can be shown that the total number of ways that GANG
can appear as a subsequence is .
when divided by
leaves a remainder of
.
This test case will be the first test case in the second subtask.
Comments