Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate, dropwhile
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    r_idx, g_idx, b_idx = [[i for i, c in enumerate(S) if c == x] for x in 'RGB']
    idxs = {'R': sorted(g_idx + b_idx), 'G': sorted(r_idx + b_idx), 'B': sorted(r_idx + g_idx)}

    ans = 0
    for i in range(N - 2):
        idxs[S[i]] = list(dropwhile(lambda x: i > x, idxs[S[i]]))
        for j in idxs[S[i]]:
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            k = j + (j - i)
            ans += sums[other][-1] - sums[other][j]
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
from sys import stdin
from itertools import accumulate


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline().rstrip()
    r_sum, g_sum, b_sum = [list(accumulate(1 if c == x else 0 for c in S)) for x in 'RGB']

    ans = 0
    sums = {'R': r_sum, 'G': g_sum, 'B': b_sum}
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            if S[i] == S[j]:
                continue
            other = 'RGB'.replace(S[i], '').replace(S[j], '')
            ans += sums[other][-1] - sums[other][j]
            k = j + (j - i)
            if k < N and S[k] == other:
                ans -= 1
    print(ans)


if __name__ == '__main__':
    main()