Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce

n = int(input())
s = input().strip()

MOD = 998244353

R = 1
G = 2
B = 4
RG = R + G
RB = R + B
GB = G + B
RGB = R + G + B
table = {'R': R, 'G': G, 'B': B}
counts = {
    0: 0,
    R: 0, G: 0, B: 0,
    RG: 0, RB: 0, GB: 0,
}
patterns = 1
for cv in [table[c] for c in s]:
    rest = RGB - cv
    if counts[rest] > 0:
        patterns *= counts[rest]
        counts[rest] -= 1
    elif sum([counts[v] for v in [R, G, B] if v != cv]) > 0:
        for bc in [R, G, B]:
            if rest & bc != 0 and counts[bc] > 0:
                patterns *= counts[bc]
                counts[bc] -= 1
                counts[bc + cv] += 1
                break
    else:
        counts[cv] += 1

print((patterns * reduce(lambda o, v: o * v, range(1, n + 1), 1)) % MOD)


Fixed submission:
from functools import reduce

n = int(input())
s = input().strip()

MOD = 998244353

R = 1
G = 2
B = 4
RG = R + G
RB = R + B
GB = G + B
RGB = R + G + B
table = {'R': R, 'G': G, 'B': B}
counts = {
    0: 0,
    R: 0, G: 0, B: 0,
    RG: 0, RB: 0, GB: 0,
}
patterns = 1
for cv in [table[c] for c in s]:
    rest = RGB - cv
    if counts[rest] > 0:
        patterns = (patterns * counts[rest]) % MOD
        counts[rest] -= 1
    elif R != cv and counts[R] > 0:
        patterns = (patterns * counts[R]) % MOD
        counts[R] -= 1
        counts[R + cv] += 1
    elif G != cv and counts[G] > 0:
        patterns = (patterns * counts[G]) % MOD
        counts[G] -= 1
        counts[G + cv] += 1
    elif B != cv and counts[B] > 0:
        patterns = (patterns * counts[B]) % MOD
        counts[B] -= 1
        counts[B + cv] += 1
    else:
        counts[cv] += 1

print((patterns * reduce(lambda o, v: (o * v) % MOD, range(1, n + 1), 1)) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce

n = int(input())
s = input().strip()

MOD = 998244353

R = 1
G = 2
B = 4
RG = R + G
RB = R + B
GB = G + B
RGB = R + G + B
table = {'R': R, 'G': G, 'B': B}
counts = {
    0: 0,
    R: 0, G: 0, B: 0,
    RG: 0, RB: 0, GB: 0,
}
patterns = 1
for cv in [table[c] for c in s]:
    rest = RGB - cv
    if counts[rest] > 0:
        patterns *= counts[rest]
        counts[rest] -= 1
    elif sum([counts[v] for v in [R, G, B] if v != cv]) > 0:
        for bc in [R, G, B]:
            if rest & bc != 0 and counts[bc] > 0:
                patterns *= counts[bc]
                counts[bc] -= 1
                counts[bc + cv] += 1
                break
    else:
        counts[cv] += 1

print((patterns * reduce(lambda o, v: o * v, range(1, n + 1), 1)) % MOD)


Fixed submission:
from functools import reduce

n = int(input())
s = input().strip()

MOD = 998244353

R = 1
G = 2
B = 4
RG = R + G
RB = R + B
GB = G + B
RGB = R + G + B
table = {'R': R, 'G': G, 'B': B}
counts = {
    0: 0,
    R: 0, G: 0, B: 0,
    RG: 0, RB: 0, GB: 0,
}
patterns = 1
for cv in [table[c] for c in s]:
    rest = RGB - cv
    if counts[rest] > 0:
        patterns = (patterns * counts[rest]) % MOD
        counts[rest] -= 1
    elif R != cv and counts[R] > 0:
        patterns = (patterns * counts[R]) % MOD
        counts[R] -= 1
        counts[R + cv] += 1
    elif G != cv and counts[G] > 0:
        patterns = (patterns * counts[G]) % MOD
        counts[G] -= 1
        counts[G + cv] += 1
    elif B != cv and counts[B] > 0:
        patterns = (patterns * counts[B]) % MOD
        counts[B] -= 1
        counts[B + cv] += 1
    else:
        counts[cv] += 1

print((patterns * reduce(lambda o, v: (o * v) % MOD, range(1, n + 1), 1)) % MOD)