Buggy python submission (verdict: Time Limit Exceeded):
n= int(input())
s = input()
ans = 0
li_r = []
li_g = []
li_b = []
for i in range(n):
  if s[i] == "R":
    li_r.append(i+1)
  elif s[i] == "G":
    li_g.append(i+1)
  elif s[i] == "B":
    li_b.append(i+1)
for i in li_r:
  for j in li_g:
    if (j+i)%2 == 0:
      if 2*j -i in li_b and 2*i - j in li_b and (j+i)//2 in li_b:
        ans += len(li_b)-3
      elif 2*j -i not in li_b and 2*i - j in li_b and (j+i)//2 in li_b:
        ans += len(li_b) -2
      elif 2*j -i in li_b and 2*i - j not in li_b and (j+i)//2 in li_b:
        ans += len(li_b) -2
      elif 2*j -i in li_b and 2*i - j in li_b and (j+i)//2 not in li_b:
        ans += len(li_b) -2
      elif 2*j -i not in li_b and 2*i - j not in li_b and (j+i)//2 in li_b:
        ans += len(li_b) -1
      elif 2*j -i in li_b and 2*i - j not in li_b and (j+i)//2 not in li_b:
        ans += len(li_b) -1
      elif 2*j -i not in li_b and 2*i - j in li_b and (j+i)//2 not in li_b:
        ans += len(li_b) -1
      elif 2*j -i not in li_b and 2*i - j not in li_b and (j+i)//2 not in li_b:
        ans += len(li_b)
    else:
      if 2*j -i in li_b and 2*i - j in li_b:
        ans += len(li_b)-2
      elif 2*j -i not in li_b and 2*i - j in li_b:
        ans += len(li_b) -1
      elif 2*j -i in li_b and 2*i - j not in li_b:
        ans += len(li_b) -1
      elif 2*j -i not in li_b and 2*i - j not in li_b:
        ans += len(li_b)
print(ans)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！
n = I()
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    for k in range(i+1,n):
        if (i+k)%2 == 0:
            j = (i+k)//2
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans+=1
ans_2 = c["R"]*c["G"]*c["B"]
print(ans_2-ans)

Buggy python submission (verdict: Time Limit Exceeded):
n= int(input())
s = input()
ans = 0
li_r = []
li_g = []
li_b = []
for i in range(n):
  if s[i] == "R":
    li_r.append(i+1)
  elif s[i] == "G":
    li_g.append(i+1)
  elif s[i] == "B":
    li_b.append(i+1)
for i in li_r:
  for j in li_g:
    for k in li_b:
      li = [i,j,k]
      li.sort()
      a = li[0]
      b = li[1]
      c = li[2]
      if b-a != c-b:
        ans += 1
print(ans)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！
n = I()
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    for k in range(i+1,n):
        if (i+k)%2 == 0:
            j = (i+k)//2
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans+=1
ans_2 = c["R"]*c["G"]*c["B"]
print(ans_2-ans)

Buggy python submission (verdict: Runtime Error):
n= int(input())
s = input()
ans = 0
li_r = []
li_g = []
li_b = []
for i in range(n):
  if li[i] == "R":
    li_r.append(i+1)
  elif li[i] == "G":
    li_g.append(i+1)
  elif li[i] == "B":
    li_b.append(i+1)
for i in li_r:
  for j in li_g:
    if 2*j - i in li_b:
      ans += len(li_b) -1
    else:
      ans += len(li_b)
print(ans)

Fixed submission:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**7)
import bisect
import heapq
import itertools
import math
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from math import gcd
from operator import add, itemgetter, mul, xor
def cmb(n,r,mod):
  bunshi=1
  bunbo=1
  for i in range(r):
    bunbo = bunbo*(i+1)%mod
    bunshi = bunshi*(n-i)%mod
  return (bunshi*pow(bunbo,mod-2,mod))%mod
mod = 10**9+7
def I(): return int(input())
def LI(): return list(map(int,input().split()))
def MI(): return map(int,input().split())
def LLI(n): return [list(map(int, input().split())) for _ in range(n)]

#bisect.bisect_left(list,key)はlistのなかでkey未満の数字がいくつあるかを返す
#つまりlist[i] < x となる i の個数
#bisect.bisect_right(list, key)はlistのなかでkey以下の数字がいくつあるかを返す
#つまりlist[i] <= x となる i の個数
#これを応用することで
#len(list) - bisect.bisect_left(list,key)はlistのなかでkey以上の数字がいくつあるかを返す
#len(list) - bisect.bisect_right(list,key)はlistのなかでkeyより大きい数字がいくつあるかを返す
#これらを使うときはあらかじめlistをソートしておくこと！
n = I()
s = input()
c = Counter(s)
ans = 0
for i in range(n):
    for k in range(i+1,n):
        if (i+k)%2 == 0:
            j = (i+k)//2
            if s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
                ans+=1
ans_2 = c["R"]*c["G"]*c["B"]
print(ans_2-ans)