Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import *

def main():
	input = sys.stdin.readline

	N = int(input())
	S = input().strip()
	M = 998244353

	ri = []
	gi = []
	bi = []

	for i, c in enumerate(S):
		if c=='R':
			ri.append(i)
		elif c=='G':
			gi.append(i)
		else:
			bi.append(i)

	si = []
	mi = []
	li = []
	for i, tt in enumerate(zip(ri,gi,bi)):
		s, m, l = sorted(tt)
		si.append(s)
		mi.append(m)
		li.append(l)

	ans = 1
	for i, mm in enumerate(mi):
		st = bisect(si,mm)
		lt = bisect(li,mm)
		ans *= (st-i)*(i+1-lt)*(i+1)%M

	print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from bisect import *

def main():
	input = sys.stdin.readline

	N = int(input())
	S = input().strip()
	M = 998244353

	ri = []
	gi = []
	bi = []

	for i, c in enumerate(S):
		if c=='R':
			ri.append(i)
		elif c=='G':
			gi.append(i)
		else:
			bi.append(i)

	si = []
	mi = []
	li = []
	for i, tt in enumerate(zip(ri,gi,bi)):
		s, m, l = sorted(tt)
		si.append(s)
		mi.append(m)
		li.append(l)

	ans = 1
	for i, mm in enumerate(mi):
		st = bisect(si,mm)
		lt = bisect(li,mm)
		ans = ans*(st-i)*(i+1-lt)*(i+1)
		ans = ans%M

	print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import *

def main():
	input = sys.stdin.readline

	N = int(input())
	S = input().strip()
	M = 998244353

	ri = []
	gi = []
	bi = []

	for i, c in enumerate(S):
		if c=='R':
			ri.append(i)
		elif c=='G':
			gi.append(i)
		else:
			bi.append(i)

	si = []
	mi = []
	li = []
	for i in range(N):
		tt = [ri[i],gi[i],bi[i]]
		tt.sort()
		si.append(tt[0])
		mi.append(tt[1])
		li.append(tt[2])

	ans = 1
	for i in range(N):
		st = bisect(si,mi[i])
		lt = bisect(li,mi[i])
		ans *= (st-i)*(i+1-lt)*(i+1)%M

	print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
import sys
from bisect import *

def main():
	input = sys.stdin.readline

	N = int(input())
	S = input().strip()
	M = 998244353

	ri = []
	gi = []
	bi = []

	for i, c in enumerate(S):
		if c=='R':
			ri.append(i)
		elif c=='G':
			gi.append(i)
		else:
			bi.append(i)

	si = []
	mi = []
	li = []
	for i, tt in enumerate(zip(ri,gi,bi)):
		s, m, l = sorted(tt)
		si.append(s)
		mi.append(m)
		li.append(l)

	ans = 1
	for i, mm in enumerate(mi):
		st = bisect(si,mm)
		lt = bisect(li,mm)
		ans = ans*(st-i)*(i+1-lt)*(i+1)
		ans = ans%M

	print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from bisect import *

input = sys.stdin.readline

N = int(input())
S = input().strip()
M = 998244353

ri = []
gi = []
bi = []

for i, c in enumerate(S):
	if c=='R':
		ri.append(i)
	elif c=='G':
		gi.append(i)
	else:
		bi.append(i)

si = []
mi = []
li = []
for i in range(N):
	tt = [ri[i],gi[i],bi[i]]
	tt.sort()
	si.append(tt[0])
	mi.append(tt[1])
	li.append(tt[2])

ans = 1
for i in range(N):
	st = bisect(si,mi[i])
	lt = bisect(li,mi[i])
	ans *= (st-i)*(i+1-lt)*(i+1)%M

print(ans)

Fixed submission:
import sys
from bisect import *

def main():
	input = sys.stdin.readline

	N = int(input())
	S = input().strip()
	M = 998244353

	ri = []
	gi = []
	bi = []

	for i, c in enumerate(S):
		if c=='R':
			ri.append(i)
		elif c=='G':
			gi.append(i)
		else:
			bi.append(i)

	si = []
	mi = []
	li = []
	for i, tt in enumerate(zip(ri,gi,bi)):
		s, m, l = sorted(tt)
		si.append(s)
		mi.append(m)
		li.append(l)

	ans = 1
	for i, mm in enumerate(mi):
		st = bisect(si,mm)
		lt = bisect(li,mm)
		ans = ans*(st-i)*(i+1-lt)*(i+1)
		ans = ans%M

	print(ans)

if __name__ == "__main__":
    main()