Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import itertools

def binary_search(left, right, search, List):
    while right-left > 1:
        mid = (left+right)//2
        if List[mid] == search:
            return mid
        else:
            if search > List[mid]:
                left = mid
            else:
                right = mid
    return -1


n = int(input())
s = input()

d = {"R": [], "G": [], "B": []}

for i in range(n):
    d[s[i]].append(i)

p=list(itertools.permutations(["R","G","B"]))

ans = 0
for a,b,c in p:
    for r in range(len(d[a])):
        second = bisect.bisect_left(d[b], d[a][r])
        for g in range(second, len(d[b])):
            third = bisect.bisect_left(d[c], d[b][g])
            ans += len(d[c])-third
            if binary_search(-1,len(d[c]),2*d[b][g]-d[a][r],d[c])!=-1:
                ans-=1

print(ans)

Fixed submission:
n = int(input())
s = input()
r=0
g=0
b=0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="B":
        b+=1
    else:
        g+=1
ans=r*b*g

for i in range(n):
    for g in range(i+1,n):
        if s[i]!=s[g] and 2*g-i<n:
            if s[2*g-i]!=s[i] and s[2*g-i]!=s[g]:
                ans-=1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import itertools

def binary_search(left, right, search, List):
    while right-left > 1:
        mid = (left+right)//2
        if List[mid] == search:
            return mid
        else:
            if search > List[mid]:
                left = mid
            else:
                right = mid
    return -1


n = int(input())
s = input()

d = {"R": [], "G": [], "B": []}

for i in range(n):
    d[s[i]].append(i)

p=list(itertools.permutations(["R","G","B"]))

ans = 0
for a,b,c in p:
    for r in range(len(d[a])):
        second = bisect.bisect_left(d[b], d[a][r])
        for g in range(second, len(d[b])):
            third = bisect.bisect_left(d[c], d[b][g])
            ans += len(d[c])-third
            if binary_search(-1,len(d[c]),2*d[b][g]-d[a][r],d[c])!=-1:
                ans-=1

print(ans)


Fixed submission:
n = int(input())
s = input()
r=0
g=0
b=0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="B":
        b+=1
    else:
        g+=1
ans=r*b*g

for i in range(n):
    for g in range(i+1,n):
        if s[i]!=s[g] and 2*g-i<n:
            if s[2*g-i]!=s[i] and s[2*g-i]!=s[g]:
                ans-=1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import itertools

def binary_search(left, right, search, List):
    while right-left > 1:
        mid = (left+right)//2
        if List[mid] == search:
            return mid
        else:
            if search > List[mid]:
                left = mid
            else:
                right = mid
    return -1


n = int(input())
s = input()

d = {"R": [], "G": [], "B": []}

for i in range(n):
    d[s[i]].append(i)

p=list(itertools.permutations(["R","G","B"]))

ans = 0
for a,b,c in p:
    for r in range(len(d[a])):
        second = bisect.bisect_left(d[b], d[a][r])
        for g in range(second, len(d[b])):
            third = bisect.bisect_left(d[c], d[b][g])
            ans += len(d[c])-third
            if binary_search(-1,len(d[c]),2*d[b][g]-d[a][r],d[c])!=-1:
                ans-=1

print(ans)

Fixed submission:
n = int(input())
s = input()
r=0
g=0
b=0
for i in range(n):
    if s[i]=="R":
        r+=1
    elif s[i]=="B":
        b+=1
    else:
        g+=1
ans=r*b*g

for i in range(n):
    for g in range(i+1,n):
        if s[i]!=s[g] and 2*g-i<n:
            if s[2*g-i]!=s[i] and s[2*g-i]!=s[g]:
                ans-=1

print(ans)