ABC Ultimatum

AtCoder
IDagc055_d
Time2000ms
Memory256MB
Difficulty
Let's call a string $T$ of length $3N$, containing exactly $N$ letters `A`, $N$ letters `B`, and $N$ letters `C`, **good**, if it can be split into $N$ (not necessarily contiguous) subsequences of length $3$, so that the letters from each subsequence form `ABC`, `BCA`, or `CAB`. You are given a string $S$ of length $3N$, 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 $998244353$. ## Constraints * $1\le N \le 15$. * $S$ is a string of length $3N$, consisting of `A`, `B`, `C`, and `?`. ## Input Input is given from Standard Input in the following format: $N$ $S$ [samples]
Samples
Input #1
1
???
Output #1
3

There are $3$ good strings you can obtain: `ABC`, `BCA`, and `CAB`.
Input #2
2
AA????
Output #2
2

There are $2$ good strings you can obtain: `AABBCC` and `AABCBC`.
Input #3
3
?A?A?A?A?
Output #3
0

It's impossible to obtain a good string, as there are already $4$ `A`'s.
Input #4
9
?????????A??B??C???????????
Output #4
331653164
API Response (JSON)
{
  "problem": {
    "name": "ABC Ultimatum",
    "description": {
      "content": "Let's call a string $T$ of length $3N$, containing exactly $N$ letters `A`, $N$ letters `B`, and $N$ letters `C`, **good**, if it can be split into $N$ (not necessarily contiguous) subsequences of len",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "agc055_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Let's call a string $T$ of length $3N$, containing exactly $N$ letters `A`, $N$ letters `B`, and $N$ letters `C`, **good**, if it can be split into $N$ (not necessarily contiguous) subsequences of len...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments