ABA and BAB

AtCoder
IDarc180_a
Time2000ms
Memory256MB
Difficulty
You are given a string $S$ of length $N$ consisting of characters `A` and `B`. You can perform the following two types of operations zero or more times in any order: * Choose a (contiguous) substring `ABA` in $S$ and replace it with `A`. * Choose a (contiguous) substring `BAB` in $S$ and replace it with `B`. Find, modulo $10^9+7$, the number of possible strings $S$ after performing the operations. ## Constraints * $1 \leq N \leq 250000$ * $S$ is a string of length $N$ consisting of characters `A` and `B`. ## Input The input is given from Standard Input in the following format: $N$ $S$ [samples]
Samples
Input #1
4
ABAB
Output #1
2

The two possible strings $S$ after the operations are as follows:

*   `ABAB`: This string can be obtained by performing zero operations.
*   `AB`: The 1st to 3rd characters of $S=$`ABAB` are `ABA`. Replacing them with `A` results in $S=$`AB`.

Here, the 2nd to 4th characters of $S=$`ABAB` are `BAB`, so you can also replace them with `B`. Note, however, that the resulting `AB` does not count multiple times.
Input #2
1
A
Output #2
1

No operations can be performed.
Input #3
17
BBABABAABABAAAABA
Output #3
18
Input #4
100
ABAABAABABBABAABAABAABABBABBABBABBABBABBABBABBABBABBABBABBABBABBABAABABAABABBABBABABBABAABAABAABAABA
Output #4
415919090

Remember to take the result modulo $10^9+7$.
API Response (JSON)
{
  "problem": {
    "name": "ABA and BAB",
    "description": {
      "content": "You are given a string $S$ of length $N$ consisting of characters `A` and `B`. You can perform the following two types of operations zero or more times in any order: *   Choose a (contiguous) substri",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc180_a"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a string $S$ of length $N$ consisting of characters `A` and `B`.\nYou can perform the following two types of operations zero or more times in any order:\n\n*   Choose a (contiguous) substri...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments