Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append(pow3[-1] * 3)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))

Fixed submission:
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append((pow3[-1] * 3) % MOD)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))

Buggy python submission (verdict: Runtime Error):
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = Counter({})
back = Counter(S)
pow3 = [1]
for i in range(back['?']):
  pow3.append(pow3[-1] * 3)

for i in range(len(S)):
  c = S[i]
  back[c] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    # ?を使わない
    ans += front['A'] * back['C'] * (pow3[front['?'] + back['?']])
    # Aだけ?をつかう
    ans += front['?'] * back['C'] * (pow3[front['?'] + back['?'] - 1])
    # Cだけ?をつかう
    ans += front['A'] * back['?'] * (pow3[front['?'] + back['?'] - 1])
    # 両方?をつかう
    ans += front['?'] * back['?'] * (pow3[front['?'] + back['?'] - 2])
    ans %= MOD
  front[c] += 1

print(int(ans))

Fixed submission:
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append((pow3[-1] * 3) % MOD)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = Counter({})
back = Counter(S)
for i in range(len(S)):
  c = S[i]
  back[c] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    # ?を使わない
    ans += front['A'] * back['C'] * (3 ** (front['?'] + back['?']))
    # Aだけ?をつかう
    ans += front['?'] * back['C'] * (3 ** (front['?'] + back['?'] - 1))
    # Cだけ?をつかう
    ans += front['A'] * back['?'] * (3 ** (front['?'] + back['?'] - 1))
    # 両方?をつかう
    ans += front['?'] * back['?'] * (3 ** (front['?'] + back['?'] - 2))
    ans %= MOD
  front[c] += 1

print(int(ans))

Fixed submission:
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append((pow3[-1] * 3) % MOD)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = Counter({})
back = Counter(S)
for i in range(len(S)):
  c = S[i]
  back[c] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    # ?を使わない
    ans += front['A'] * back['C'] * (3 ** (front['?'] + back['?']))
    # Aだけ?をつかう
    ans += front['?'] * back['C'] * (3 ** (front['?'] + back['?'] - 1))
    # Cだけ?をつかう
    ans += front['A'] * back['?'] * (3 ** (front['?'] + back['?'] - 1))
    # 両方?をつかう
    ans += front['?'] * back['?'] * (3 ** (front['?'] + back['?'] - 2))
    ans %= MOD
  front[c] += 1

print(int(ans))

Fixed submission:
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append((pow3[-1] * 3) % MOD)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
for i in range(len(S)):
  # Bの位置を固定
  if S[i] == 'B' or S[i] == '?':
    front = Counter(S[:i])
    back = Counter(S[i+1:])
    # ?を使わない
    ans += front['A'] * back['C'] * (3 ** (front['?'] + back['?']))
    # Aだけ?をつかう
    ans += front['?'] * back['C'] * (3 ** (front['?'] + back['?'] - 1))
    # Cだけ?をつかう
    ans += front['A'] * back['?'] * (3 ** (front['?'] + back['?'] - 1))
    # 両方?をつかう
    ans += front['?'] * back['?'] * (3 ** (front['?'] + back['?'] - 2))
    ans %= MOD

print(int(ans))

Fixed submission:
from collections import Counter
S = input()
MOD = 10 ** 9 + 7
ans = 0
front = [0, 0, 0, 0]
back = [0, 0, 0, 0]
m = { 'A': 0, 'B': 1, 'C': 2, '?': 3 }
for c in S:
  back[m[c]] += 1

pow3 = [1]
for i in range(max(3, back[3])):
  pow3.append((pow3[-1] * 3) % MOD)

for i in range(len(S)):
  c = S[i]
  back[m[c]] -= 1
  # Bの位置を固定
  if c == 'B' or c == '?':
    fbsum = front[3] + back[3]
    # ?を使わない
    ans += front[0] * back[2] * (pow3[fbsum])
    # Aだけ?をつかう
    ans += front[3] * back[2] * (pow3[fbsum - 1])
    # Cだけ?をつかう
    ans += front[0] * back[3] * (pow3[fbsum - 1])
    # 両方?をつかう
    ans += front[3] * back[3] * (pow3[fbsum - 2])
    ans %= MOD
  front[m[c]] += 1

print(int(ans))