#P4199. 万径人踪灭
万径人踪灭
Description
If the computer room is about to close, or you are in a hurry, please skip directly to the sixth paragraph.
VFleaKing noticed that along the dirt road going up and down the mountain, some places offer beautiful views while others do not. Divide the uphill road into segments of cm each. For every segment, use a to indicate that a nice view can be enjoyed, and b to indicate that it cannot. Then you get a string containing only a and b. Since going downhill uses the same road, the string for the downhill road is the reverse of the uphill string.
Let the uphill string have length , with characters in order. On the way up and down, VFleaKing will choose certain segments to look at the scenery, and keep his head down the rest of the time. That is, VFleaKing chooses segments , where and , and he will look at the scenery at these positions both uphill and downhill.

VFleaKing wants the results of seeing scenery to be the same uphill and downhill. That is, the sequence of whether he sees scenery uphill is , denoted by , and the sequence downhill is , denoted by . VFleaKing wants .
VFleaKing also wants the intervals between these checks to be equal uphill and downhill. That is, the uphill intervals are , denoted by . The downhill intervals are $x_k - x_{k-1}, x_{k-1} - x_{k-2}, \ldots, x_2 - x_1$, denoted by . VFleaKing wants .
VFleaKing thinks that if, between the first and the last time he looks at the scenery, there is not a single moment when he keeps his head down, he will fall. That is, if for all we have , then VFleaKing will fall, and he does not want this to happen.
In short, select a subsequence from a string containing only a and b such that:
- Both positions and characters are symmetric about some axis.
- It is not a contiguous segment.
Take as an example. If we use to denote a sequence, then is a valid sequence , is also valid, and is also valid. However, does not satisfy VFleaKing’s first and third wishes, so it is not valid. does not satisfy the second wish, so it is not valid. does not satisfy the third wish, so it is not valid.

Given the string , VFleaKing wants to know how many valid sequences there are. The answer can be large; please output it modulo .
Input Format
One line, a string containing only the two characters a and b.
Output Format
One line, a non-negative integer representing the answer.
abaabaa
14
aaabbbaaa
44
aaaaaaaa
53
Hint
Sample Explanation 1
There are valid sequences:
- , , , , , , , , ;
- , ;
- , , .
Sample Explanation 2
I have come up with a brilliant explanation, but there are too many cases to fit here.
Sample Explanation 3
I have come up with a brilliant explanation, but there are too many cases to fit here.
Constraints
- For of the testdata, the string contains only the letter
aor only the letterb. - For another of the testdata, .
- For another of the testdata, either the number of
adoes not exceed , or the number ofbdoes not exceed . - For another of the testdata, .
- For of the testdata, .
Source
- 2013 Hubei Mutual Test week 1
- bzoj 3160
- A Mathematical Textbook for the Informatics Olympiad
- Compiled by stong9070
Translated by ChatGPT 5
京公网安备 11011102002149号