33OJ
登录 注册

#P17551. [JAG 2026 Summer Camp #2] Take Many ABA

[JAG 2026 Summer Camp #2] Take Many ABA

Problem Description

For a string TT consisting of A and B, define f(T)f(T) as the maximum number of times the following operation can be performed.

Operation: Remove ABA that appears as a (not necessarily contiguous) subsequence of TT, and close up the resulting gaps.

You are given a string SS consisting of A, B, and ?. Find the sum of f(T)f(T) over all strings TT obtained by replacing each ? in SS with either A or B, modulo 998244353998244353.

Input Format

The input is given in the following format:

S

SS is a string consisting of A, B, and ?, and its length is between 33 and 10001000, inclusive.

Output Format

Print the answer.

A?B?
2
?B?A?B?
15
????????????????????
4595728

Hint

The possible strings TT and their corresponding values of f(T)f(T) for the first sample are as follows:

  • T=AABAT=\texttt{AABA}: f(T)=1f(T)=1.
  • T=AABBT=\texttt{AABB}: f(T)=0f(T)=0.
  • T=ABBAT=\texttt{ABBA}: f(T)=1f(T)=1.
  • T=ABBBT=\texttt{ABBB}: f(T)=0f(T)=0.

Therefore, the answer is 1+0+1+0=21+0+1+0=2.