Buggy python submission (verdict: Runtime Error):
s = input()
dp = [0 for _ in range(0,4)]
dp[3] = 1
dp[0] = 0
dp[1] = 0
dp[2] = 0
abc = "ABC".index
mod = 10**9 + 7
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp = [
        (dp[0]*3+ dp[1])%mod
        ,(dp[1]*3+ dp[2])%mod
        ,(dp[2]*3 + dp[3])%mod
        ,(dp[3]*3)%mod
        ]
    else:
        num = abc(s[i])
        dp[i][num] += dp[i+1][num+1] % mod
# for i in range(len(s),-1,-1):
#     print(dp[i])
print(dp[0][0] %mod)


Fixed submission:
s = input()
dp = [0 for _ in range(0,4)]
dp[3] = 1
dp[0] = 0
dp[1] = 0
dp[2] = 0
abc = "ABC".index
mod = 10**9 + 7
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp = [
        (dp[0]*3+ dp[1])%mod
        ,(dp[1]*3+ dp[2])%mod
        ,(dp[2]*3 + dp[3])%mod
        ,(dp[3]*3)%mod
        ]
    else:
        num = abc(s[i])
        dp[num] += dp[num+1] % mod
print(dp[0] %mod)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
dp = [[0 for _ in range(0,4)] for _ in range(0,len(s)+1)]
dp[len(s)][3] = 1
dp[len(s)][0] = 0
dp[len(s)][1] = 0
dp[len(s)][2] = 0
abc = "ABC".index
for i in range(len(s)-1,-1,-1):
    m = 1
    if s[i] ==  "?":
        m=3
    dp[i][3] += m * dp[i+1][3]
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp[i] = [
        dp[i+1][0]*3+ dp[i+1][1]
        ,dp[i+1][1]*3+ dp[i+1][2]
        ,dp[i+1][2]*3 + dp[i+1][3]
        ,dp[i+1][3]*3
        ]
    else:
        dp[i] = [
        dp[i+1][0]
        ,dp[i+1][1]
        ,dp[i+1][2]
        ,dp[i+1][3]
        ]
        num = abc(s[i])
        dp[i][num] += dp[i+1][num+1]
# for i in range(len(s),-1,-1):
#     print(dp[i])
print(dp[0][0] % (10**9 + 7))


Fixed submission:
s = input()
dp = [0 for _ in range(0,4)]
dp[3] = 1
dp[0] = 0
dp[1] = 0
dp[2] = 0
abc = "ABC".index
mod = 10**9 + 7
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp = [
        (dp[0]*3+ dp[1])%mod
        ,(dp[1]*3+ dp[2])%mod
        ,(dp[2]*3 + dp[3])%mod
        ,(dp[3]*3)%mod
        ]
    else:
        num = abc(s[i])
        dp[num] += dp[num+1] % mod
print(dp[0] %mod)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
dp = [[0 for _ in range(0,4)] for _ in range(0,len(s)+1)]
dp[len(s)][3] = 1
dp[len(s)][0] = 0
dp[len(s)][1] = 0
dp[len(s)][2] = 0
abc = ["A","B","C"]
for i in range(len(s)-1,-1,-1):
    m = 1
    if s[i] ==  "?":
        m=3
    dp[i][3] += m * dp[i+1][3]

for i in range(len(s)-1,-1,-1):
    for j in range(0,3):
        m1,m2 = 1,0
        if s[i] == "?":
            m1,m2 = 3,1
        if s[i] == abc[j]:
            m1,m2 = 1,1
        dp[i][j] += m1 * dp[i+1][j] + m2 * dp[i+1][j+1]
        dp[i][j] %= (10**9 + 7)
# for i in range(len(s),-1,-1):
#     print(dp[i])
print(dp[0][0] % (10**9 + 7))


Fixed submission:
s = input()
dp = [0 for _ in range(0,4)]
dp[3] = 1
dp[0] = 0
dp[1] = 0
dp[2] = 0
abc = "ABC".index
mod = 10**9 + 7
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp = [
        (dp[0]*3+ dp[1])%mod
        ,(dp[1]*3+ dp[2])%mod
        ,(dp[2]*3 + dp[3])%mod
        ,(dp[3]*3)%mod
        ]
    else:
        num = abc(s[i])
        dp[num] += dp[num+1] % mod
print(dp[0] %mod)

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
dp = [[0 for _ in range(0,4)] for _ in range(0,len(s)+1)]
dp[len(s)][3] = 1
dp[len(s)][0] = 0
dp[len(s)][1] = 0
dp[len(s)][2] = 0
abc = ["A","B","C"]
for i in range(len(s)-1,-1,-1):
    m = 1
    if s[i] ==  "?":
        m=3
    dp[i][3] += m * dp[i+1][3]

for i in range(len(s)-1,-1,-1):
    for j in range(0,3):
        m1,m2 = 1,0
        if s[i] == "?":
            m1,m2 = 3,1
        if s[i] == abc[j]:
            m1,m2 = 1,1
        dp[i][j] += m1 * dp[i+1][j] + m2 * dp[i+1][j+1]
# for i in range(len(s),-1,-1):
#     print(dp[i])
print(dp[0][0] % (10**9 + 7))


Fixed submission:
s = input()
dp = [0 for _ in range(0,4)]
dp[3] = 1
dp[0] = 0
dp[1] = 0
dp[2] = 0
abc = "ABC".index
mod = 10**9 + 7
for i in range(len(s)-1,-1,-1):
    if s[i] == "?":
        dp = [
        (dp[0]*3+ dp[1])%mod
        ,(dp[1]*3+ dp[2])%mod
        ,(dp[2]*3 + dp[3])%mod
        ,(dp[3]*3)%mod
        ]
    else:
        num = abc(s[i])
        dp[num] += dp[num+1] % mod
print(dp[0] %mod)