Buggy python submission (verdict: Runtime Error):
def calc():
  n=int(input())
  s=list(input())
  cnt1=0
  cnt2=0
  cnt3=0
  for i in range(n):
      if s[i]=='R':
          cnt1+=1
          s[i]=1
      elif s[i]=='G':
          cnt2+=1
          s[i]=2
      elif s[i]=='B':
          cnt3+=1
          s[i]=4
  ans=cnt1*cnt2*cnt3
  for i in range(n):
      for j in range(i+1,n):
        k=1*j-i
          if k>=n:
            continue
          if s[i]+s[j]+s[k]==7:
            ans-=1
  print(ans)
calc()

Fixed submission:
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=input()
cnt=[0,0,0]
for i in range(n):
    if s[i]=='R':
        cnt[0]+=1
    elif s[i]=='G':
        cnt[1]+=1
    elif s[i]=='B':
        cnt[2]+=1
ans=cnt[0]*cnt[1]*cnt[2]
for i in range(n):
    for j in range(i+1,n):
        if 2*j-i>=n:
          continue
        if len(set([s[i],s[j],s[2*j-i]]))==3:
          ans-=1
print(ans)

Fixed submission:
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)

Fixed submission:
n=int(input())
s=input()
cnt=[[0,0,0] for _ in range(n)]
for i in range(n):
    if s[i]=='R':
        cnt[i][0]+=1
    elif s[i]=='G':
        cnt[i][1]+=1
    elif s[i]=='B':
        cnt[i][2]+=1
acum=[[0,0,0]]
for i in range(n):
    tmp=[0,0,0]
    for j in range(3):
        tmp[j]+=acum[-1][j]
        tmp[j]+=cnt[i][j]
    acum.append(tmp)
ans=0
for i in range(n):
    for j in range(i+1,n):
        if s[i]==s[j]:
            continue
        if 'R' not in (s[i],s[j]):
            ans+=acum[-1][0]-acum[j][0]
            if 2*j-i<n and s[2*j-i]=='R':
                ans-=1
        elif 'G' not in (s[i],s[j]):
            ans+=acum[-1][1]-acum[j][1]
            if 2*j-i<n and s[2*j-i]=='G':
                ans-=1
        elif 'B' not in (s[i],s[j]):
            ans+=acum[-1][2]-acum[j][2]
            if 2*j-i<n and s[2*j-i]=='B':
                ans-=1
print(ans)