Buggy python submission (verdict: Runtime Error):
md=10**9+7
s=input()
dp=[[0]*4 for _ in range(3)]
for c in s:
    if c=="A":dp[0][0]=(dp[0][0]+1)%md
    if c=="B":
        for j in range(2):dp[1][j]=(dp[1][j]+dp[0][j])%md
    if c=="C":
        for j in range(3):dp[2][j]=(dp[2][j]+dp[1][j])%md
    if c=="?":
        for j in range(3):dp[2][j+1]=(dp[2][j+1]+dp[1][j])%md
        for j in range(2):dp[1][j+1]=(dp[1][j+1]+dp[0][j])%md
        dp[0][1] = (dp[0][1] + 1) % md
ans=0
q=s.count("?")
for j in range(4):
    ans+=dp[2][j]*pow(3,q-j,md)
    ans%=md
print(ans)


Fixed submission:
md=10**9+7
s=input()
dp=[[0]*4 for _ in range(3)]
for c in s:
    if c=="A":dp[0][0]=(dp[0][0]+1)%md
    if c=="B":
        for j in range(2):dp[1][j]=(dp[1][j]+dp[0][j])%md
    if c=="C":
        for j in range(3):dp[2][j]=(dp[2][j]+dp[1][j])%md
    if c=="?":
        for j in range(3):dp[2][j+1]=(dp[2][j+1]+dp[1][j])%md
        for j in range(2):dp[1][j+1]=(dp[1][j+1]+dp[0][j])%md
        dp[0][1] = (dp[0][1] + 1) % md
ans=0
q=s.count("?")
for j in range(4):
    if q-j<0:break
    ans+=dp[2][j]*pow(3,q-j,md)
    ans%=md
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def f(s):
    n = len(s)
    dp = [[0, 0, 0, 0] for _ in range(n + 1)]
    pre = dp[0]
    for i, c in enumerate(s):
        cur = dp[i + 1]
        if c == "A":
            cur[0] = (pre[0] + 3 ** pre[3]) % md
            cur[1:] = pre[1:]
        elif c == "B":
            cur[0] = pre[0]
            cur[1] = (pre[0] + pre[1]) % md
            cur[2:] = pre[2:]
        elif c == "C":
            cur[:2] = pre[:2]
            cur[2] = (pre[1] + pre[2]) % md
            cur[3] = pre[3]
        else:
            cur[0] = (pre[0] * 3 + 3 ** pre[3]) % md
            cur[1] = (pre[1] * 3 + pre[0]) % md
            cur[2] = (pre[2] * 3 + pre[1]) % md
            cur[3] = (pre[3] + 1) % md
        pre = cur
    print(dp[-1][-2])

md = 10 ** 9 + 7
s = input()
f(s)


Fixed submission:
md=10**9+7
s=input()
dp=[[0]*4 for _ in range(3)]
for c in s:
    if c=="A":dp[0][0]=(dp[0][0]+1)%md
    if c=="B":
        for j in range(2):dp[1][j]=(dp[1][j]+dp[0][j])%md
    if c=="C":
        for j in range(3):dp[2][j]=(dp[2][j]+dp[1][j])%md
    if c=="?":
        for j in range(3):dp[2][j+1]=(dp[2][j+1]+dp[1][j])%md
        for j in range(2):dp[1][j+1]=(dp[1][j+1]+dp[0][j])%md
        dp[0][1] = (dp[0][1] + 1) % md
ans=0
q=s.count("?")
for j in range(4):
    if q-j<0:break
    ans+=dp[2][j]*pow(3,q-j,md)
    ans%=md
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
md = 10 ** 9 + 7
s = input()
n = len(s)
dp = [[0, 0, 0, 0] for _ in range(n + 1)]
pre = dp[0]
for i, c in enumerate(s):
    cur = dp[i + 1]
    if c == "A":
        cur[0] = (pre[0] + 3 ** pre[3]) % md
        cur[1:] = pre[1:]
    elif c == "B":
        cur[0] = pre[0]
        cur[1] = (pre[0] + pre[1]) % md
        cur[2:] = pre[2:]
    elif c == "C":
        cur[:2] = pre[:2]
        cur[2] = (pre[1] + pre[2]) % md
        cur[3] = pre[3]
    else:
        cur[0] = (pre[0] * 3 + 3 ** pre[3]) % md
        cur[1] = (pre[1] * 3 + pre[0]) % md
        cur[2] = (pre[2] * 3 + pre[1]) % md
        cur[3] = (pre[3] + 1) % md
    pre = cur
print(dp[-1][-2])


Fixed submission:
md=10**9+7
s=input()
dp=[[0]*4 for _ in range(3)]
for c in s:
    if c=="A":dp[0][0]=(dp[0][0]+1)%md
    if c=="B":
        for j in range(2):dp[1][j]=(dp[1][j]+dp[0][j])%md
    if c=="C":
        for j in range(3):dp[2][j]=(dp[2][j]+dp[1][j])%md
    if c=="?":
        for j in range(3):dp[2][j+1]=(dp[2][j+1]+dp[1][j])%md
        for j in range(2):dp[1][j+1]=(dp[1][j+1]+dp[0][j])%md
        dp[0][1] = (dp[0][1] + 1) % md
ans=0
q=s.count("?")
for j in range(4):
    if q-j<0:break
    ans+=dp[2][j]*pow(3,q-j,md)
    ans%=md
print(ans)