output_description: Print the number of ways in which Takahashi can distribute the balls, modulo
  998244353.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      S
attempt: import sys
  
  sys.setrecursionlimit(10**6)
  
  
  def main():
      input = sys.stdin.readline
      N = int(input())
      S = str(input().strip())
      MOD = 998244353
  
      # state = (_, r, g, b, rg, gb, br)
      def f(i, ans, state):
          if i > 3 * N - 1:
              return ans
          if min(state) < 0:
              return 0
  
          no_ball, r, g, b, rg, gb, br = state
          if S[i] == "R":
              if gb > 0:
                  ans = (ans * gb) % MOD
                  return f(i + 1, ans, [no_ball, r, g, b, rg, gb - 1, br])
              elif g > 0 or b > 0:
                  ans = ans * (g + b) % MOD
                  s1 = [no_ball, r, g - 1, b, rg + 1, gb, br]
                  s2 = [no_ball, r, g, b - 1, rg, gb, br + 1]
                  return (f(i + 1, ans, s1) + f(i + 1, ans, s2)) % MOD
              else:
                  ans = (ans * no_ball) % MOD
                  return f(i + 1, ans, [no_ball - 1, r + 1, g, b, rg, gb, br])
  
          elif S[i] == "G":
              if br > 0:
                  ans = (ans * br) % MOD
                  return f(i + 1, ans, [no_ball, r, g, b, rg, gb, br - 1])
              elif r > 0 or b > 0:
                  ans = ans * (r + b) % MOD
                  s1 = [no_ball, r - 1, g, b, rg + 1, gb, br]
                  s2 = [no_ball, r, g, b - 1, rg, gb + 1, br]
                  return (f(i + 1, ans, s1) + f(i + 1, ans, s2)) % MOD
              else:
                  ans = (ans * no_ball) % MOD
                  return f(i + 1, ans, [no_ball - 1, r, g + 1, b, rg, gb, br])
  
          else:  # S[i] == 'B':
              if rg > 0:
                  ans = (ans * rg) % MOD
                  return f(i + 1, ans, [no_ball, r, g, b, rg - 1, gb, br])
              elif r > 0 or g > 0:
                  ans = ans * (r + g) % MOD
                  s1 = [no_ball, r - 1, g, b, rg, gb, br + 1]
                  s2 = [no_ball, r, g - 1, b, rg, gb + 1, br]
                  return (f(i + 1, ans, s1) + f(i + 1, ans, s2)) % MOD
              else:
                  ans = (ans * no_ball) % MOD
                  return f(i + 1, ans, [no_ball - 1, r, g, b + 1, rg, gb, br])
  
      return f(0, 1, [N, 0, 0, 0, 0, 0, 0]) % MOD
  
  
  if __name__ == "__main__":
      print(main())
  
problem_description: Statement
  
  We have 3N colored balls with IDs from 1 to 3N. A string S of length 3N
  represents the colors of the balls. The color of Ball i is red if S_i is `R`,
  green if S_i is `G`, and blue if S_i is `B`. There are N red balls, N green
  balls, and N blue balls.
  
  Takahashi will distribute these 3N balls to N 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 a_j < b_j < c_j be the IDs of the balls received by the j-th person in ascending order.
    * Then, \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 998244353. We consider two ways to
  distribute the balls different if and only if there is a person who receives
  different sets of balls.
samples: [{"input": "3\n    RRRGGGBBB", "output": "216\n    \n\nThe minimum value of \\sum_j (c_j-a_j) is 18 when the balls are, for example,\ndistributed as follows:\n\n  * The first person gets Ball 1, 5, and 9.\n  * The second person gets Ball 2, 4, and 8.\n  * The third person gets Ball 3, 6, and 7.\n\n* * *"}, {"input": "5\n    BBRGRRGRGGRBBGB", "output": "960"}]