Problem p02940 (python)

Fixed submission:
import sys
input = sys.stdin.readline

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

mod=998244353

NOW=dict()
NOW["R"]=0
NOW["G"]=0
NOW["B"]=0

ANS=1
for i in range(1,N+1):
    ANS=ANS*i%mod

for s in S:
    k,l,m=sorted((NOW["R"],NOW["G"],NOW["B"]))
    
    if NOW[s]==m:
        NOW[s]+=1

    elif NOW[s]==l:
        NOW[s]+=1
        ANS=ANS*(m-l)%mod

    else:
        NOW[s]+=1
        ANS=ANS*l%mod
    
    if NOW["R"]>=1 and NOW["G"]>=1 and NOW["B"]>=1:
        NOW["R"]-=1
        NOW["G"]-=1
        NOW["B"]-=1

print(ANS)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline

N=int(input())
S=input().strip()+"E"

mod=998244353


S2=deque()
NOW=1
C=S[0]
for i in range(1,3*N+1):
    if S[i]==C:
        NOW+=1
    else:
        S2.append([C,NOW])
        NOW=1
        C=S[i]

LEN=len(S2)

ANS=1
for i in range(1,N+1):
    ANS=ANS*i%mod

NOW=[]
for i in range(3):
    x,y=S2.popleft()
    NOW.append([x,y])

#print(NOW)
while S2 or NOW:
    if len(S2)>0 and len(NOW)>0 and NOW[-1][0]==S2[0][0]:
        x,y=S2.popleft()
        NOW[-1][1]+=y

    while len(NOW)<=2:
        x,y=S2.popleft()
        NOW.append([x,y])

    ANS=ANS*NOW[0][1]*min(NOW[1][1],NOW[0][1])%mod
    NOW[0][1]-=1
    NOW[1][1]-=1
    NOW[2][1]-=1

    while len(NOW)>=3 and NOW[2][1]==0:
        NOW.pop(2)

    while len(NOW)>=2 and NOW[1][1]==0:
        NOW.pop(1)

    while len(NOW)>=1 and NOW[0][1]==0:
        NOW.pop(0)

    #print(NOW)
        
print(ANS)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
input = sys.stdin.readline

N=int(input())
S=input().strip()+"E"

mod=998244353


S2=deque()
NOW=1
C=S[0]
for i in range(1,3*N+1):
    if S[i]==C:
        NOW+=1
    else:
        S2.append([C,NOW])
        NOW=1
        C=S[i]

LEN=len(S2)

ANS=1
for i in range(1,N+1):
    ANS=ANS*i%mod

NOW=[]
for i in range(3):
    x,y=S2.popleft()
    NOW.append([x,y])

#print(NOW)
while S2 or NOW:
    if len(S2)>0 and NOW[-1][0]==S2[0][0]:
        x,y=S2.popleft()
        NOW[-1][1]+=y

    while len(NOW)<=2:
        x,y=S2.popleft()
        NOW.append([x,y])

    ANS=ANS*NOW[0][1]*min(NOW[1][1],NOW[0][1])%mod
    NOW[0][1]-=1
    NOW[1][1]-=1
    NOW[2][1]-=1

    while len(NOW)>=3 and NOW[2][1]==0:
        NOW.pop(2)

    while len(NOW)>=2 and NOW[1][1]==0:
        NOW.pop(1)

    while len(NOW)>=1 and NOW[0][1]==0:
        NOW.pop(0)

    #print(NOW)
        
print(ANS)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N=int(input())
S=input().strip()+"E"

mod=998244353


FACT=[1]
for i in range(1,2*10**5+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(2*10**5,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0


S2=[]
NOW=1
C=S[0]
for i in range(1,3*N+1):
    if S[i]==C:
        NOW+=1
    else:
        S2.append((C,NOW))
        NOW=1
        C=S[i]

from collections import deque
Q=deque(S2)


ANS=1
INV=1
while Q:
    x0,x1=Q.popleft()
    y0,y1=Q.popleft()
    z0,z1=Q.popleft()

    ANS=(ANS*x1*y1*z1)%mod
    INV=INV*pow(x1,mod-2,mod)

    if Q and Q[0][0]==z0:
        w0,w1=Q.popleft()

        Q.appendleft(w0,w1+z1-1)

    elif z1!=1:
        Q.appendleft((z0,z1-1))
        
    if y1!=1:
        Q.appendleft((y0,y1-1))
    if x1!=1:
        Q.appendleft((x0,x1-1))

print(ANS*INV*FACT[N]%mod)