Problem p02714 (python)

Fixed submission:
from bisect import bisect_right
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if Ron[i]==1:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(B)>0:
                ind1 = bisect_right(B,indg)
                if 2*indg-indr>B[-1] or Bon[2*indg-indr]==0 or Bon[2*indg-indr]==1 and 2*indg-indr<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(G)>0:
                ind1 = bisect_right(G,indb)
                if 2*indb-indr>G[-1] or Gon[2*indb-indr]==0 or Gon[2*indb-indr]==1 and 2*indb-indr<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
    elif Gon[i]==1:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            if len(R)>0:
                ind1 = bisect_right(R,indb)
                if 2*indb-indg>R[-1] or Ron[2*indb-indg]==0 or Ron[2*indb-indg]==1 and 2*indb-indg<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(B)>0:
                ind1 = bisect_right(B,indr)
                if 2*indr-indg>B[-1] or Bon[2*indr-indg]==0 or Bon[2*indr-indg]==1 and 2*indr-indg<B[ind1]:
                    cnt += len(B)-ind1
                else:
                    cnt += len(B)-ind1-1
    elif Bon[i]==1:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            if len(G)>0:
                ind1 = bisect_right(G,indr)
                if 2*indr-indb>G[-1] or Gon[2*indr-indb]==0 or Gon[2*indr-indb]==1 and 2*indr-indb<G[ind1]:
                    cnt += len(G)-ind1
                else:
                    cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            if len(R)>0:
                ind1 = bisect_right(R,indg)
                if 2*indg-indb>R[-1] or Ron[2*indg-indb]==0 or Ron[2*indg-indb]==1 and 2*indg-indb<R[ind1]:
                    cnt += len(R)-ind1
                else:
                    cnt += len(R)-ind1-1
print(cnt)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if Ron[i]==1:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(B,indg)
            if 2*indg-indr>B[-1] or Bon[2*indg-indr]==0 or Bon[2*indg-indr]==1 and 2*indg-indr<B[ind1]:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(G,indb)
            if 2*indb-indr>G[-1] or Gon[2*indb-indr]==0 or Gon[2*indb-indr]==1 and 2*indb-indr<G[ind1]:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
    elif Gon[i]==1:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(R,indb)
            if 2*indb-indg>R[-1] or Ron[2*indb-indg]==0 or Ron[2*indb-indg]==1 and 2*indb-indg<R[ind1]:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(B,indr)
            if 2*indr-indg>B[-1] or Bon[2*indr-indg]==0 or Bon[2*indr-indg]==1 and 2*indr-indg<B[ind1]:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
    elif Bon[i]==1:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(G,indr)
            if 2*indr-indb>G[-1] or Gon[2*indr-indb]==0 or Gon[2*indr-indb]==1 and 2*indr-indb<G[ind1]:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(R,indg)
            if 2*indg-indb>R[-1] or Ron[2*indg-indb]==0 or Ron[2*indg-indb]==1 and 2*indg-indb<R[ind1]:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
print(cnt)

Buggy submission (verdict: Runtime Error):
from bisect import bisect_right,bisect_left
N = int(input())
S = input().strip()
R = []
G = []
B = []
Ron = [0 for _ in range(len(S))]
Gon = [0 for _ in range(len(S))]
Bon = [0 for _ in range(len(S))]
for i in range(N):
    if S[i]=="R":
        R.append(i)
        Ron[i]=1
    elif S[i]=="G":
        G.append(i)
        Gon[i]=1
    else:
        B.append(i)
        Bon[i]=1
cnt = 0
for i in range(N):
    if i in R:
        #RGB
        indr = i
        ind0 = bisect_right(G,indr)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(B,indg)
            if Bon[2*indg-indr]==0:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
        #RBG
        ind0 = bisect_right(B,indr)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(G,indb)
            if Gon[2*indb-indr]==0:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
    elif i in G:
        #GBR
        indg = i
        ind0 = bisect_right(B,indg)
        for j in range(ind0,len(B)):
            indb = B[j]
            ind1 = bisect_right(R,indb)
            if Ron[2*indb-indg]==0:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
        #GRB
        ind0 = bisect_right(R,indg)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(B,indr)
            if Bon[2*indr-indg]==0:
                cnt += len(B)-ind1
            else:
                cnt += len(B)-ind1-1
    elif i in B:
        #BRG
        indb = i
        ind0 = bisect_right(R,indb)
        for j in range(ind0,len(R)):
            indr = R[j]
            ind1 = bisect_right(G,indr)
            if Gon[2*indr-indb]==0:
                cnt += len(G)-ind1
            else:
                cnt += len(G)-ind1-1
        #BGR
        ind0 = bisect_right(G,indb)
        for j in range(ind0,len(G)):
            indg = G[j]
            ind1 = bisect_right(R,indg)
            if Ron[2*indg-indb]==0:
                cnt += len(R)-ind1
            else:
                cnt += len(R)-ind1-1
print(cnt)