Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = 0
N_C = 0
for c in S:
    if(c == "?"):
        N_Q += 1
    elif(c == "C"):
        N_C += 1

AQ_0 = int(3**N_Q)
AQ_1 = AQ_0 // 3
AQ_2 = AQ_1 // 3

BQ_0 = AQ_0 // 3
BQ_1 = AQ_1 // 3
BQ_2 = AQ_2 // 3


t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

ans = 0
baac = 0
bqaq = 0
aqqc = 0
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    bA = t_A
    bQ = t_Q
    if(S[i:i + 1] == "?"):
        aQ = N_Q - t_Q - 1
    else:
        aQ = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    aC = t_C

    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue
    elif(S[i:i + 1] == "?"):
        Q_0 = BQ_0
        Q_1 = BQ_1
        Q_2 = BQ_2
    else:
        Q_0 = AQ_0
        Q_1 = AQ_1
        Q_2 = AQ_2

    # q_0
    baac += bA * aC * Q_0
    # q_2
    bqaq += bQ * aQ * Q_2
    # Q_1
    aqqc += (bA * aQ + bQ * aC) * Q_1

ans = (ans + baac) % max_S
ans = (ans + bqaq) % max_S
ans = (ans + aqqc) % max_S


print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = 0
N_C = 0
for c in S:
    if(c == "?"):
        N_Q += 1
    elif(c == "C"):
        N_C += 1

AQ_0 = int(3**N_Q)
AQ_1 = AQ_0 // 3
AQ_2 = AQ_1 // 3

BQ_0 = AQ_0 // 3
BQ_1 = AQ_1 // 3
BQ_2 = AQ_2 // 3


t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

ans = 0
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    bA = t_A
    bQ = t_Q
    if(S[i:i + 1] == "?"):
        aQ = N_Q - t_Q - 1
    else:
        aQ = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    aC = t_C

    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue
    elif(S[i:i + 1] == "?"):
        Q_0 = BQ_0
        Q_1 = BQ_1
        Q_2 = BQ_2
    else:
        Q_0 = AQ_0
        Q_1 = AQ_1
        Q_2 = AQ_2

    ans += Q_0 * bA * aC + bQ * aQ * Q_2 + Q_1 * (bA * aQ + bQ * aC)

ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = S.count("?")
N_C = S.count("C")

AQ_0 = int(3**N_Q)
AQ_1 = AQ_0 // 3
AQ_2 = AQ_1 // 3

BQ_0 = AQ_0//3
BQ_1 = AQ_1//3
BQ_2 = AQ_2//3


t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

ans = 0
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    bA = t_A
    bQ = t_Q
    if(S[i:i + 1] == "?"):
        aQ = N_Q - t_Q - 1
    else:
        aQ = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    aC = t_C

    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue
    elif(S[i:i+1] == "?"):
        Q_0 = BQ_0
        Q_1 = BQ_1
        Q_2 = BQ_2
    else:
        Q_0 = AQ_0
        Q_1 = AQ_1
        Q_2 = AQ_2

    ans += Q_0 * bA * aC + bQ * aQ * Q_2 + Q_1 * (bA * aQ + bQ * aC)
    ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = S.count("?")
N_C = S.count("C")

t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

ans = 0
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    bA = t_A
    bQ = t_Q
    if(S[i:i + 1] == "?"):
        aQ = N_Q - t_Q - 1
    else:
        aQ = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    aC = t_C

    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue

    Q_0 = int(3**(bQ + aQ))
    if(Q_0 == 1):
        Q_1 = 0
        Q_2 = 0
    elif(Q_0 == 3):
        Q_1 = 1
        Q_2 = 0
    else:
        Q_1 = Q_0 // 3
        Q_2 = Q_1 // 3
    ans += Q_0 * bA * aC + bQ * aQ * Q_2 + Q_1 * (bA * aQ + bQ * aC)
    ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = S.count("?")
N_C = S.count("C")

t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

S_list = [{"bA": t_A, "bQ": t_Q, "aC": t_C, "aQ": N_Q - t_Q}]
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    ba_dic = {}
    ba_dic["bA"] = t_A
    ba_dic["bQ"] = t_Q
    if(S[i:i + 1] == "?"):
        ba_dic["aQ"] = N_Q - t_Q - 1
    else:
        ba_dic["aQ"] = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    ba_dic["aC"] = t_C

    S_list.append(ba_dic)

ans = 0
for i in range(len(S)):
    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue

    bA = S_list[i]["bA"]
    bQ = S_list[i]["bQ"]
    aC = S_list[i]["aC"]
    aQ = S_list[i]["aQ"]

    Q_0 = int(3**(bQ + aQ))
    if(Q_0 == 1):
        Q_1 = 0
        Q_2 = 0
    elif(Q_0 == 3):
        Q_1 = 1
        Q_2 = 0
    else:
        Q_1 = Q_0 // 3
        Q_2 = Q_1 // 3
    ans += Q_0 * bA * aC + bQ * aQ * Q_2 + Q_1 * (bA * aQ + bQ * aC)
    ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Runtime Error):
S = input()

max_S = 10**9 + 7

N_Q = S.count("?")
N_C = S.count("C")

t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

S_list = [{"bA": t_A, "bQ": t_Q, "aC": t_C, "aQ": N_Q - t_Q}]
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    ba_dic = {}
    ba_dic["bA"] = t_A
    ba_dic["bQ"] = t_Q
    if(S[i:i + 1] == "?"):
        ba_dic["aQ"] = N_Q - t_Q - 1
    else:
        ba_dic["aQ"] = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    ba_dic["aC"] = t_C

    S_list.append(ba_dic)

ans = 0
for i in range(len(S)):
    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue

    bA = S_list[i]["bA"]
    bQ = S_list[i]["bQ"]
    aC = S_list[i]["aC"]
    aQ = S_list[i]["aQ"]

    Q_0 = int(3**(bQ + aQ))
    if(Q_0 == 1):
        Q_1 = 0
        Q_2 = 0
    elif(Q_0 == 3):
        Q_1 = 1
        Q_2 = 0
    else:
        Q_1 = Q_0 / 3
        Q_2 = Q_1 / 3
    ans += Q_0 * bA * aC + bQ * aQ * Q_2 + Q_1 * (bA * aQ + bQ * aC)
    ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()

max_S = 10**9 + 7

N_Q = S.count("?")
N_C = S.count("C")

t_A = 0
t_Q = 0
if(S[0:1] == "C"):
    t_C = N_C - 1
else:
    t_C = N_C

S_list = [{"bA": t_A, "bQ": t_Q, "aC": t_C, "aQ": N_Q - t_Q}]
for i in range(1, len(S)):
    if(S[i - 1:i] == "A"):
        t_A += 1
    elif(S[i - 1:i] == "?"):
        t_Q += 1

    ba_dic = {}
    ba_dic["bA"] = t_A
    ba_dic["bQ"] = t_Q
    if(S[i:i + 1] == "?"):
        ba_dic["aQ"] = N_Q - t_Q - 1
    else:
        ba_dic["aQ"] = N_Q - t_Q
    if(S[i:i + 1] == "C"):
        t_C -= 1
    ba_dic["aC"] = t_C

    S_list.append(ba_dic)

ans = 0
for i in range(len(S)):
    if(S[i:i + 1] == "A" or S[i:i + 1] == "C"):
        continue

    bA = S_list[i]["bA"]
    bQ = S_list[i]["bQ"]
    aC = S_list[i]["aC"]
    aQ = S_list[i]["aQ"]

    ans += bA * aC * int(3**(bQ + aQ))
    ans = ans % max_S

    ans += bQ * aQ * int(3**(bQ + aQ - 2))
    ans = ans % max_S

    ans += (bA * aQ + bQ * aC) * int(3**(bQ + aQ - 1))
    ans = ans % max_S

print(ans)

Fixed submission:
S = input()

d = [0, 0, 0, 1]

max_ans = 10**9 + 7

for c in reversed(S):
    # print(c, d)
    m = [c == "A", c == "B", c == "C", c == "?"]
    if(m[3] is True):
        tmp = 3
    else:
        tmp = 1

    for j in range(3):
        d[j] = (tmp * d[j] + int(m[j] or m[3]) * d[j + 1]) % max_ans
    d[3] = (tmp * d[3]) % max_ans

print(d[0])