Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    ######### Index
    mix = []
    for i in range(len(S)):
        for j in range(i+1, len(S)):
            for k in range(j+1, len(S)):
                s = ''.join(sorted([S[i], S[j], S[k]]))
                if s == 'BGR':
                    mix.append([i, j, k])
    
    C = 0
    out = 0

    
    # print('----------------')
    for i, j, k in mix:
        # if S[i] != 'R' or S[j] != 'G' or S[k] != 'B':
        # print('aaa:', S[i], S[j], S[k])
        
        if abs(j - i) != abs(k - j):
            if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
            
                
                out += 1
        # else:
        #     print('out:', i, j, k, S[i], S[j], S[k])


    print(out)


    
main()

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

# print(plus)
 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    ######### Index
    mix = []
    for i in range(len(S)):
        for j in range(i+1, len(S)):
            for k in range(j+1, len(S)):
                s = ''.join(sorted([S[i], S[j], S[k]]))
                if s == 'BGR':
                    mix.append([i, j, k])
    
    C = 0
    out = 0

    
    # print('----------------')
    for i, j, k in mix:
        # if S[i] != 'R' or S[j] != 'G' or S[k] != 'B':
        # print('aaa:', S[i], S[j], S[k])
        
        if abs(j - i) != abs(k - j):
            if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
            
                
                out += 1
        # else:
        #     print('out:', i, j, k, S[i], S[j], S[k])


    print(out)


    
main()

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

# print(plus)
 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # ######### Index
    # R, G, B = [], [], []
    # for i, s in enumerate(S):
    #     if s == 'R':
    #         R.append(i)
    #     elif s == 'G':
    #         G.append(i)
    #     elif s == 'B':
    #         B.append(i)
    
    C = 0
    out = 0
    # for i in R:
    # for i in B:
    # for i in range(len(S)):
        
        
    #     # for j in G:
    #     # for j in B:
    #     for j in range(i+1, len(S)):
            

    #         # for k in B:
    #         # for k in G:
    #         # for k in R:
    #         for k in range(j+1, len(S)):
                
    #             # print(i, j, k)
                
    #             if not (j - i) == (k - j):
    #                 if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
                    
                        
    #                     out += 1
    
    combinations = [[i, j, k]
                for i in range(len(S))
                for j in range(i+1, len(S))
                for k in range(j+1, len(S))
                ]
    # print(combinations)
    
    for i, j, k in combinations:
        if not (j - i) == (k - j):
            if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
            
                
                out += 1



    print(out)

    
main()

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

# print(plus)
 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # ######### Index
    # R, G, B = [], [], []
    # for i, s in enumerate(S):
    #     if s == 'R':
    #         R.append(i)
    #     elif s == 'G':
    #         G.append(i)
    #     elif s == 'B':
    #         B.append(i)
    
    C = 0
    out = 0
    # for i in R:
    # for i in B:
    for i in range(len(S)):
        
        
        # for j in G:
        # for j in B:
        for j in range(i+1, len(S)):
            

            # for k in B:
            # for k in G:
            # for k in R:
            for k in range(j+1, len(S)):
                
                # print(i, j, k)
                
                if not (j - i) == (k - j):
                    if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
                    
                        
                        out += 1


    print(out)

    
main()

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

# print(plus)
 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    # N = 39
    # S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
    
    # R, G, B = [], [], []
    # for i, s in enumerate(S):
    #     if s == 'R':
    #         R.append(i)
    #     elif s == 'G':
    #         G.append(i)
    #     elif s == 'B':
    #         B.append(i)
    C = 0
    out = 0
    # for i in R:
    # for i in B:
    for i in range(len(S)):
        
        
        # for j in G:
        # for j in B:
        for j in range(len(S)):
            
            if i >= j:
                continue
            

            # for k in B:
            # for k in G:
            # for k in R:
            for k in range(len(S)):
                
                if j >= k:
                    continue
                
                # if (1 <= i < j < k < N) and (j - i != k - j):
                #     C += 1
             
                if not (j - i) == (k - j):
                    if (S[i] != S[j]) and (S[i] != S[k]) and (S[k] != S[j]):
                    
                    # if (S[i] != S[j]) and (S[i] != S[k]) and (S[j] != S[k]):
                    #     C += 1
                        # print('1:', i, j, k)
                        
                        
                        out += 1

                # if (j - i) == (k - j):
                #     print('---------------------------')
                #     print('2:', i, j, k)
                #     print('j - i, k - j:', j - i, k - j)
    
    # print(C, out)
    # print(R)
    # print(G)
    # print(B)
    # print(len(R) * len(G) * len(B))
    print(out)

    
main()

Fixed submission:
import sys
sys.setrecursionlimit(300000)
from collections import defaultdict
 
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')
 
 
N = I()
S = input()

# N = 39
# S = 'RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB'
 
d = defaultdict(int)
for c in S:
    d[c] += 1
plus = d['R'] * d['G'] * d['B']

# print(plus)
 
minus = 0
for j in range(1, N - 1):
    for l in range(1, N):
        i, k = j - l, j + l
        if i < 0 or k > N - 1:
            break
        if S[i] != S[j] and S[j] != S[k] and S[k] != S[i]:
            minus += 1
 
print(plus - minus)