Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for i in rs:
        for j in reversed(gs):
            if not (i < j):
                break
            for k in reversed(bs):
                if not (i < k):
                    break
                if j < k:
                    if (j - i) != k - j:
                        res += 1
                else:
                    if (k - i) != j - k:
                        res += 1

    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    lgs = len(gs)
    lbs = len(bs)
    for i in rs:
        j = bisect.bisect_left(gs, i)
        k = bisect.bisect_left(bs, i)
        res += (lgs - j) * (lbs - k)

        for jj in range(j, lgs):
            d = (gs[jj] - i)
            index = bisect.bisect_left(bs, i + 2 * d)
            if index != lbs:
                if bs[index] == i + 2 * d:
                    res -= 1

        for kk in range(k, lbs):
            d = (bs[kk] - i)
            index = bisect.bisect_left(gs, i + 2 * d)
            if index != lgs:
                if gs[index] == i + 2 * d:
                    res -= 1


        # for j in reversed(gs):
        #     if not (i < j):
        #         break
        #     for k in reversed(bs):
        #         if not (i < k):
        #             break
        #         if j < k:
        #             if (j - i) != k - j:
        #                 res += 1
        #         else:
        #             if (k - i) != j - k:
        #                 res += 1

    return res


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for i in rs:
        for j in reversed(gs):
            if not (i < j):
                break
            for k in reversed(bs):
                if not (j < k):
                    break
                if (j - i) != k - j:
                    res += 1
    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(rs, bs, gs) + f(gs, rs, bs) + f(gs, bs, rs) + f(bs, rs, gs) + f(bs, gs, rs))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    lgs = len(gs)
    lbs = len(bs)
    for i in rs:
        j = bisect.bisect_left(gs, i)
        k = bisect.bisect_left(bs, i)
        res += (lgs - j) * (lbs - k)

        for jj in range(j, lgs):
            d = (gs[jj] - i)
            index = bisect.bisect_left(bs, i + 2 * d)
            if index != lbs:
                if bs[index] == i + 2 * d:
                    res -= 1

        for kk in range(k, lbs):
            d = (bs[kk] - i)
            index = bisect.bisect_left(gs, i + 2 * d)
            if index != lgs:
                if gs[index] == i + 2 * d:
                    res -= 1


        # for j in reversed(gs):
        #     if not (i < j):
        #         break
        #     for k in reversed(bs):
        #         if not (i < k):
        #             break
        #         if j < k:
        #             if (j - i) != k - j:
        #                 res += 1
        #         else:
        #             if (k - i) != j - k:
        #                 res += 1

    return res


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    for ir in range(0, len(rs)):
        for ig in range(0, len(gs)):
            for ib in range(0, len(bs)):
                i = rs[ir]
                j = gs[ig]
                k = bs[ib]
                if i < j and j < k and (j - i) != k - j:
                    res += 1
    return res
    # ir,ig,ib=(0,0,0)
    # while True:
    #     i= rs[ir]
    #     j= gs[ig]
    #     k= bs[ib]
    #     if i<j and j<k and (j-i)!=k-j:
    #         res+=1
    #


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(rs, bs, gs) + f(gs, rs, bs) + f(gs, bs, rs) + f(bs, rs, gs) + f(bs, gs, rs))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque, Counter
from itertools import chain, combinations
import json
# import numpy as np
import bisect
import sys
import math
import bisect

sys.setrecursionlimit(10 ** 8)
# mem = {}

M = 10 ** 9 + 7


def f(rs, gs, bs):
    res = 0
    lgs = len(gs)
    lbs = len(bs)
    for i in rs:
        j = bisect.bisect_left(gs, i)
        k = bisect.bisect_left(bs, i)
        res += (lgs - j) * (lbs - k)

        for jj in range(j, lgs):
            d = (gs[jj] - i)
            index = bisect.bisect_left(bs, i + 2 * d)
            if index != lbs:
                if bs[index] == i + 2 * d:
                    res -= 1

        for kk in range(k, lbs):
            d = (bs[kk] - i)
            index = bisect.bisect_left(gs, i + 2 * d)
            if index != lgs:
                if gs[index] == i + 2 * d:
                    res -= 1


        # for j in reversed(gs):
        #     if not (i < j):
        #         break
        #     for k in reversed(bs):
        #         if not (i < k):
        #             break
        #         if j < k:
        #             if (j - i) != k - j:
        #                 res += 1
        #         else:
        #             if (k - i) != j - k:
        #                 res += 1

    return res


def main():
    N = int(input())
    S = input()

    rs = []
    gs = []
    bs = []

    for i, c in enumerate(S):
        if c == 'R':
            rs.append(i)
        elif c == 'G':
            gs.append(i)
        else:
            bs.append(i)
    print(f(rs, gs, bs) + f(gs, rs, bs) + f(bs, rs, gs))


if __name__ == "__main__":
    main()