Buggy python submission (verdict: Time Limit Exceeded):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S_alta = input()
N = len(S_alta)

cnt = 0
S = []

while cnt < N:
    if cnt <= N - 2 and S_alta[cnt] == 'B' and S_alta[cnt + 1] == 'C':
        S.append('X')
        cnt += 2
    else:
        S.append(S_alta[cnt])
        cnt += 1

S = ''.join(S)
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "X":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Fixed submission:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S = input()
S = S.replace('BC', 'P')
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "P":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S = input()
S = S.replace('BC', 'P')
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "P":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)

Fixed submission:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt, pi
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

S = input()
S = S.replace('BC', 'P')
N = len(S)

str_l = []
str_0 = ''
for i in range(N):
    if S[i] == "A" or S[i] == "P":
        str_0 += S[i]
    else:
        if str_0 != '':
            str_l.append(str_0)
            str_0 = ''
if str_0 != '':
    str_l.append(str_0)

ans = 0
for st in str_l:
    opt = []
    for i in range(len(st)):
        if st[i] == 'A':
            opt.append(0)
        else:
            opt.append(1)

    imos = copy.deepcopy(opt)
    for i in range(len(st) - 1):
        imos[-i - 2] += imos[-i - 1]

    for i in range(len(st)):
        if opt[i] == 0:
            ans += imos[i]
print(ans)