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

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

r = [0] * (2 * n)
g = [0] * (2 * n)
b = [0] * (2 * n)

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

ans = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        for x, y, z in permutations(lst, 3):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

Fixed submission:
from itertools import permutations

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

r = [0] * n
g = [0] * n
b = [0] * n

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

ans = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            continue
        for x, y, z in permutations(lst):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

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

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

r = [0] * (2 * n)
g = [0] * (2 * n)
b = [0] * (2 * n)

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

ans = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        for x, y, z in permutations(lst, 3):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

Fixed submission:
from itertools import permutations

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

r = [0] * n
g = [0] * n
b = [0] * n

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

ans = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            continue
        for x, y, z in permutations(lst):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import *
from bisect import *
n = int(input())
s = input()
r = deque()
g = deque()
b = deque()
ans = 0

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

def cnt(x, y, z):
    global ans
    for i in x:
        index1 = bisect_left(y, i)
        if index1 <= len(y) - 1:
            for j in range(index1, len(y)):
                index2 = bisect_left(z, y[j])
                if index2 <= len(z) - 1:
                    for k in range(index2, len(z)):
                        if z[k] == y[j] + (y[j] - i):
                            ans -= 1
                    ans += len(z) - index2
cnt(r, g, b)
cnt(r, b, g)
cnt(g, r, b)
cnt(g, b, r)
cnt(b, r, g)
cnt(b, g, r)
print(ans)

Fixed submission:
from itertools import permutations

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

r = [0] * n
g = [0] * n
b = [0] * n

for i in range(n):
    if s[i] == "R":
        r[i] = 1
    if s[i] == "G":
        g[i] = 1
    if s[i] == "B":
        b[i] = 1

ans = sum(r) * sum(g) * sum(b)

lst = [r, g, b]
for i in range(n - 1):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k >= n:
            continue
        for x, y, z in permutations(lst):
            if x[i] and y[j] and z[k]:
                ans -= 1
print(ans)