Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import bisect
n = ni()
s = ns()

r,g,b = [],[],[]

for i in range(n):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)

ans = 0


for i in range(n):
    if s[i] == 'R':
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(b,g[j])
            ans += (len(b)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'B':
                ans -= 1
        y = bisect.bisect_right(b,i)
        for j in range(y,len(b)):
            q = bisect.bisect_right(g,b[j])
            ans += (len(g)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'G':
                ans -= 1
    elif s[i] == 'G':
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(b,r[j])
            ans += (len(b)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'B':
                ans -= 1
        p = bisect.bisect_right(b,i)
        for j in range(p,len(b)):
            q = bisect.bisect_right(r,b[j])
            ans += (len(r)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'R':
                ans -= 1
    else:
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(r,g[j])
            ans += (len(r)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'R':
                ans -= 1
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(g,r[j])
            ans += (len(g)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'G':
                ans -= 1
    
print(ans)

Fixed submission:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import bisect
n = ni()
s = ns()

r,g,b = [],[],[]

for i in range(n):
    if s[i] == 'R':
        r.append(i)
    elif s[i] == 'G':
        g.append(i)
    else:
        b.append(i)

ans = 0


for i in range(n):
    if s[i] == 'R':
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(b,g[j])
            ans += (len(b)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'B':
                ans -= 1
        y = bisect.bisect_right(b,i)
        for j in range(y,len(b)):
            q = bisect.bisect_right(g,b[j])
            ans += (len(g)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'G':
                ans -= 1
    elif s[i] == 'G':
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(b,r[j])
            ans += (len(b)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'B':
                ans -= 1
        p = bisect.bisect_right(b,i)
        for j in range(p,len(b)):
            q = bisect.bisect_right(r,b[j])
            ans += (len(r)-q)
            if b[j]*2-i < n and s[b[j]*2-i] == 'R':
                ans -= 1
    else:
        p = bisect.bisect_right(g,i)
        for j in range(p,len(g)):
            q = bisect.bisect_right(r,g[j])
            ans += (len(r)-q)
            if g[j]*2-i < n and s[g[j]*2-i] == 'R':
                ans -= 1
        p = bisect.bisect_right(r,i)
        for j in range(p,len(r)):
            q = bisect.bisect_right(g,r[j])
            ans += (len(g)-q)
            if r[j]*2-i < n and s[r[j]*2-i] == 'G':
                ans -= 1
    
print(ans)