atcoder#AGC037B. [AGC037B] RGB Balls

[AGC037B] RGB Balls

Score : 800800 points

Problem Statement

We have 3N3N colored balls with IDs from 11 to 3N3N. A string SS of length 3N3N represents the colors of the balls. The color of Ball ii is red if SiS_i is R, green if SiS_i is G, and blue if SiS_i is B. There are NN red balls, NN green balls, and NN blue balls.

Takahashi will distribute these 3N3N balls to NN people so that each person gets one red ball, one blue ball, and one green ball. The people want balls with IDs close to each other, so he will additionally satisfy the following condition:

  • Let aj<bj<cja_j < b_j < c_j be the IDs of the balls received by the jj-th person in ascending order.
  • Then, j(cjaj)\sum_j (c_j-a_j) should be as small as possible.

Find the number of ways in which Takahashi can distribute the balls. Since the answer can be enormous, compute it modulo 998244353998244353. We consider two ways to distribute the balls different if and only if there is a person who receives different sets of balls.

Constraints

  • 1N1051 \leq N \leq 10^5
  • S=3N|S|=3N
  • SS consists of R, G, and B, and each of these characters occurs NN times in SS.

Input

Input is given from Standard Input in the following format:

NN

SS

Output

Print the number of ways in which Takahashi can distribute the balls, modulo 998244353998244353.

3
RRRGGGBBB
216

The minimum value of j(cjaj)\sum_j (c_j-a_j) is 1818 when the balls are, for example, distributed as follows:

  • The first person gets Ball 11, 55, and 99.
  • The second person gets Ball 22, 44, and 88.
  • The third person gets Ball 33, 66, and 77.
5
BBRGRRGRGGRBBGB
960