Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ) * mult(rightC, rightQ)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    else:
        leftA += (1 if S[i] == "A" else 0)
        rightC -= (1 if S[i] == "C" else 0)
print(count)

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1)) % mod

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        leftpatt, rightpatt = mult(leftA, leftQ, mod), mult(rightC, rightQ, mod)
        leftQ += (1 if S[i] == "?" else 0)
        count += leftpatt * rightpatt
        count %= mod
    else:
        leftA += (1 if S[i] == "A" else 0)
        rightC -= (1 if S[i] == "C" else 0)
print(count)

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        if leftQ >= 1:
            leftpatt = leftA * (3 ** leftQ) + leftQ * (3 ** (leftQ - 1))
        else:
            leftpatt = leftA
        if rightQ >= 1:
            rightpatt = rightC * (3 ** rightQ) + rightQ * (3 ** (rightQ - 1))
        else:
            rightpatt = rightC
        leftQ += (1 if S[i] == "?" else 0)
        count += (leftpatt * rightpatt) % mod
        count %= mod
    else:
        leftA += (1 if S[i] == "A" else 0)
        rightC -= (1 if S[i] == "C" else 0)
print(int(count))

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

for i in range(1,len(S)-1):
    if S[i] == "A":
        leftA += 1
    elif S[i] == "C":
        rightC -= 1
    elif S[i] == "B":
        if leftQ >= 1:
            leftpatt = leftA * (3 ** leftQ) + leftQ * (3 ** (leftQ - 1))
        else:
            leftpatt = leftA
        if rightQ >= 1:
            rightpatt = rightC * (3 ** rightQ) + rightQ * (3 ** (rightQ - 1))
        else:
            rightpatt = rightC
        count += (leftpatt * rightpatt) % mod
        count %= mod
    else:
        rightQ -= 1
        if leftQ >= 1:
            leftpatt = leftA * (3 ** leftQ) + leftQ * (3 ** (leftQ - 1))
        else:
            leftpatt = leftA
        if rightQ >= 1:
            rightpatt = rightC * (3 ** rightQ) + rightQ * (3 ** (rightQ - 1))
        else:
            rightpatt = rightC
        leftQ += 1
        count += (leftpatt * rightpatt) % mod
        count %= mod
print(int(count))

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Runtime Error):
S = input()
mod = 10 ** 9 + 7
def DP(i, j, S, mod):
    if i == len(S) and j == 3:
        return 1
    elif i == len(S) and j < 3:
        return 0
    elif i < len(S) and j == 3:
        if S[i] == "?":
            return 3 * DP(i+1, 3, S, mod) % mod
        else:
            return DP(i+1, 3, S, mod) % mod
    elif i < len(S) and j < 3:
        ABC = "ABC"
        if S[i] == "?":
            return (3 * DP(i+1, j, S, mod) + DP(i+1, j+1, S, mod)) % mod
        elif S[i] == ABC[j]:
            return (DP(i+1, j, S, mod) + DP(i+1, j+1, S, mod)) % mod
        else:
            return DP(i+1, j, S, mod) % mod

print(DP(0, 0, S, mod))

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Runtime Error):
S = input()
mod = 10 ** 9 + 7
def DP(i, j, S):
    if i == len(S) and j == 3:
        return 1
    elif i == len(S) and j < 3:
        return 0
    elif i < len(S) and j == 3:
        if S[i] == "?":
            return 3 * DP(i+1, 3, S) 
        else:
            return DP(i+1, 3, S)
    elif i < len(S) and j < 3:
        ABC = "ABC"
        if S[i] == "?":
            return 3 * DP(i+1, j, S) + DP(i+1, j+1, S)
        elif S[i] == ABC[j]:
            return DP(i+1, j, S) + DP(i+1, j+1, S)
        else:
            return DP(i+1, j, S)

print(DP(0, 0, S)%mod)

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        leftA, leftQ = S[:i].count("A"), S[:i].count("?")
        rightC, rightQ = S[i+1:].count("C"), S[i+1:].count("?")
        if leftQ >= 1:
            leftpatt = leftA * (3 ** leftQ) + leftQ * (3 ** (leftQ - 1))
        else:
            leftpatt = leftA
        if rightQ >= 1:
            rightpatt = rightC * (3 ** rightQ) + rightQ * (3 ** (rightQ - 1))
        else:
            rightpatt = rightC
        count += leftpatt * rightpatt
        count %= mod
print(int(count))

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
mod = 10 ** 9 + 7
count = 0
for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        left, right = list(S[:i]), list(S[i+1:])
        leftA, leftQ = left.count("A"), left.count("?")
        rightC, rightQ = right.count("C"), right.count("?")
        if leftQ >= 1:
            leftpatt = leftA * (3 ** leftQ) + leftQ * (3 ** (leftQ - 1))
        else:
            leftpatt = leftA
        if rightQ >= 1:
            rightpatt = rightC * (3 ** rightQ) + rightQ * (3 ** (rightQ - 1))
        else:
            rightpatt = rightC
        count += leftpatt * rightpatt
        count %= mod
print(int(count))

Fixed submission:
S = input()
mod = 10 ** 9 + 7
count = 0
leftA = (1 if S[0] == "A" else 0)
leftQ = (1 if S[0] == "?" else 0)
rightC = S[1:].count("C")
rightQ = S[1:].count("?")

def mult(a, b, mod):
    if b == 0:
        return a
    else:
        return ((3*a + b) * pow(3, b-1, mod))

for i in range(1,len(S)-1):
    if S[i] == "B" or S[i] == "?":
        rightQ -= (1 if S[i] == "?" else 0)
        count += mult(leftA, leftQ, mod) * mult(rightC, rightQ, mod)
        count %= mod
        leftQ += (1 if S[i] == "?" else 0)
    elif S[i] == "A":
        leftA += 1 
    else:
        rightC -= 1
print(count)