Buggy python submission (verdict: Runtime Error):
S = input()
l = len(S)
Q = S.count('?')
ans = 0
for i in range(l-2):
    for j in range(l-i-2):
        for k in range(l-i-j-2):
            d = 3**Q
            s1 = S[i]; s2 = S[i+j+1]; s3 = S[i+j+k+2]
            if s1 == 'A' or s1 == '?':
                if s1 == '?':
                    d /= 3
                if s2 == 'B' or s2 == '?':
                    if s2 == '?':
                        d /= 3
                    if s3 == 'C' or s3 == '?':
                        if s3 == '?':
                            d /= 3
                        d %= 10**9 + 7
                        ans += d
                        ans = ans%(10**9+7)
print(int(ans))

Fixed submission:
S = input()
memo = [[0,0,0,0] for i in range(len(S))]
Q = 0
memo[0][0] = 1
if S[0] == 'A':
  memo[0][1] = 1
elif S[0] == '?':
  memo[0][0] = 3
  memo[0][1] = 1
l = 1
while l < len(S):
  s = S[l]
  if s == 'A':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1] + memo[l-1][0]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3]
  elif s == 'B':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2] + memo[l-1][1]
    memo[l][3] = memo[l-1][3]
  elif s == 'C':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3] + memo[l-1][2]
  else:
    memo[l][0] = memo[l-1][0]*3
    memo[l][1] = memo[l-1][1]*3 + memo[l-1][0]
    memo[l][2] = memo[l-1][2]*3 + memo[l-1][1]
    memo[l][3] = memo[l-1][3]*3 + memo[l-1][2]
    Q += 1
  memo[l] = [i%(10**9+7) for i in memo[l]]
  l += 1
print(int(memo[l-1][3]))

Buggy python submission (verdict: Runtime Error):
S = input()
l = len(S)
Q = S.count('?')
ans = 0
for i in range(l-2):
    for j in range(l-i-2):
        for k in range(l-i-j-2):
            d = 3**Q
            s1 = S[i]; s2 = S[i+j+1]; s3 = S[i+j+k+2]
            if s1 == 'A' or s1 == '?':
                if s1 == '?':
                    d /= 3
                if s2 == 'B' or s2 == '?':
                    if s2 == '?':
                        d /= 3
                    if s3 == 'C' or s3 == '?':
                        if s3 == '?':
                            d /= 3
                        ans += d
                        ans = ans%(10**9+7)
print(int(ans))

Fixed submission:
S = input()
memo = [[0,0,0,0] for i in range(len(S))]
Q = 0
memo[0][0] = 1
if S[0] == 'A':
  memo[0][1] = 1
elif S[0] == '?':
  memo[0][0] = 3
  memo[0][1] = 1
l = 1
while l < len(S):
  s = S[l]
  if s == 'A':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1] + memo[l-1][0]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3]
  elif s == 'B':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2] + memo[l-1][1]
    memo[l][3] = memo[l-1][3]
  elif s == 'C':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3] + memo[l-1][2]
  else:
    memo[l][0] = memo[l-1][0]*3
    memo[l][1] = memo[l-1][1]*3 + memo[l-1][0]
    memo[l][2] = memo[l-1][2]*3 + memo[l-1][1]
    memo[l][3] = memo[l-1][3]*3 + memo[l-1][2]
    Q += 1
  memo[l] = [i%(10**9+7) for i in memo[l]]
  l += 1
print(int(memo[l-1][3]))

Buggy python submission (verdict: Runtime Error):
S = input()
l = len(S)
Q = S.count('?')
ans = 0
for i in range(l):
    for j in range(l-i-1):
        for k in range(l-i-j-2):
            d = 3**Q
            s1 = S[i]; s2 = S[i+j+1]; s3 = S[i+j+k+2]
            if s1 == 'A' or s1 == '?':
                if s1 == '?':
                    d /= 3
                if s2 == 'B' or s2 == '?':
                    if s2 == '?':
                        d /= 3
                    if s3 == 'C' or s3 == '?':
                        if s3 == '?':
                            d /= 3
                        ans += d
                        ans = ans%(10**9+7)
print(int(ans))

Fixed submission:
S = input()
memo = [[0,0,0,0] for i in range(len(S))]
Q = 0
memo[0][0] = 1
if S[0] == 'A':
  memo[0][1] = 1
elif S[0] == '?':
  memo[0][0] = 3
  memo[0][1] = 1
l = 1
while l < len(S):
  s = S[l]
  if s == 'A':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1] + memo[l-1][0]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3]
  elif s == 'B':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2] + memo[l-1][1]
    memo[l][3] = memo[l-1][3]
  elif s == 'C':
    memo[l][0] = memo[l-1][0]
    memo[l][1] = memo[l-1][1]
    memo[l][2] = memo[l-1][2]
    memo[l][3] = memo[l-1][3] + memo[l-1][2]
  else:
    memo[l][0] = memo[l-1][0]*3
    memo[l][1] = memo[l-1][1]*3 + memo[l-1][0]
    memo[l][2] = memo[l-1][2]*3 + memo[l-1][1]
    memo[l][3] = memo[l-1][3]*3 + memo[l-1][2]
    Q += 1
  memo[l] = [i%(10**9+7) for i in memo[l]]
  l += 1
print(int(memo[l-1][3]))