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

n = int(input())
s = input()
r, g, b = [], [], []

for i, char in enumerate(s):
    if char == 'R': r.append(i)
    elif char == 'G': g.append(i)
    else: b.append(i)

count = 0
# for config in product(r,g,b):
#     a, b, c = config
#     a, b, c = sorted((a, b, c))

#     if (b-a)!=(c-b): count += 1

# print(count)

for i in r:
    for j in g:
        for k in b:
            if i<j<k:
                if (j-i)!=(k-j): count += 1
            if j<i<k:
                if (i-j)!=(k-i): count += 1
            if k<i<j:
                if (i-k)!=(j-i): count += 1
            if i<k<j:
                if (k-i)!=(j-k): count += 1
            if j<k<i:
                if (k-j)!=(i-k): count += 1
            if k<j<i:
                if (j-k)!=(i-j): count += 1
            

print(count)


Fixed submission:
n = int(input())
s = input()

rinds, ginds, binds = [], [], []

for i in range(n):
    if s[i] == 'R': rinds.append(i)
    elif s[i] == 'G': ginds.append(i)
    else: binds.append(i)


rlen, glen, blen = len(rinds), len(ginds), len(binds)

def binsearch_same(binds, blen, dist, i):
    lo, hi = 0, blen-1
    while lo<=hi:
        mid = lo + (hi-lo)//2
        d = binds[mid] - i
        if dist == d:
            return True
        elif d < dist:
            lo = mid + 1
        else:
            hi = mid - 1
    return False

ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        # i' < j' < k

        if binsearch_same(binds, blen, dist, j_):
            ans -= 1
        # i'< k < j' 
        if dist%2==0 and binsearch_same(binds, blen, dist//2, i_):
            ans -= 1
        # # k < i' < j'
        if binsearch_same(binds, blen, -dist, i_):
            ans -= 1

print(ans)

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

n = int(input())
s = input()
r, g, b = [], [], []

for i, char in enumerate(s):
    if char == 'R': r.append(i)
    elif char == 'G': g.append(i)
    else: b.append(i)

count = 0
for config in product(r,g,b):
    a, b, c = config
    a, b, c = sorted((a, b, c))

    if (b-a)!=(c-b): count += 1

print(count)


Fixed submission:
n = int(input())
s = input()

rinds, ginds, binds = [], [], []

for i in range(n):
    if s[i] == 'R': rinds.append(i)
    elif s[i] == 'G': ginds.append(i)
    else: binds.append(i)


rlen, glen, blen = len(rinds), len(ginds), len(binds)

def binsearch_same(binds, blen, dist, i):
    lo, hi = 0, blen-1
    while lo<=hi:
        mid = lo + (hi-lo)//2
        d = binds[mid] - i
        if dist == d:
            return True
        elif d < dist:
            lo = mid + 1
        else:
            hi = mid - 1
    return False

ans = 0

for i in rinds:
    for j in ginds:
        dist = abs(i-j)
        i_ = min(i, j)
        j_ = max(i, j)
        ans += blen
        # i' < j' < k

        if binsearch_same(binds, blen, dist, j_):
            ans -= 1
        # i'< k < j' 
        if dist%2==0 and binsearch_same(binds, blen, dist//2, i_):
            ans -= 1
        # # k < i' < j'
        if binsearch_same(binds, blen, -dist, i_):
            ans -= 1

print(ans)