Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
print(DP[len(S)-1][2]%mod)

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = list(input())
Q=0
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Q=1
else:
    DP.append([0,0,0])
for i,s in enumerate(S):
    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+3**Q)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(3**Q+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Q+=1
print(DP[len(S)-1][2]%mod)

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = list(input())
Q=0
DP=[[0]*3 for i in range(len(S))]#DP[x][y] x+1文字目までの
#y=0,1,2,3 = A,AB,ABC
if S[0] == "A" or S[0]=="?":
    DP[0] = [1,0,0]
    if S[0]=="?":
        Q=1
else:
    DP[0] = [0,0,0]
for i,s in enumerate(S):
    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP[i]=[(a+3**Q)%mod,b,c]
    elif s=="B":
        DP[i]=[a,(a+b)%mod,c]
    elif s=="C":
        DP[i]=[a,b,(b+c)%mod]
    else:
        DP[i]=[(3**Q+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod]
        Q+=1
print(DP[len(S)-1][2]%mod)

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = input()
Q=0
DP=[[0]*3 for i in range(len(S))]#DP[x][y] x+1文字目までの
#y=0,1,2,3 = A,AB,ABC
if S[0] == "A" or S[0]=="?":
    DP[0] = [1,0,0]
    if S[0]=="?":
        Q=1
else:
    DP[0] = [0,0,0]
for i,s in enumerate(S):
    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP[i]=[(a+3**Q)%mod,b,c]
    elif s=="B":
        DP[i]=[a,(a+b)%mod,c]
    elif s=="C":
        DP[i]=[a,b,(b+c)%mod]
    else:
        DP[i]=[(3**Q+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod]
        Q+=1
print(DP[len(S)-1][2]%mod)

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = input()
Q=0
DP=[[0]*3 for i in range(len(S))]#DP[x][y] x+1文字目までの
#y=0,1,2,3 = A,AB,ABC
if S[0] == "A" or S[0]=="?":
    DP[0] = [1,0,0]
    if S[0]=="?":
        Q=1
else:
    DP[0] = [0,0,0]
for i in range(1,len(S)):
    s = S[i]
    a,b,c = DP[i-1]
    if s=="A":
        DP[i]=[a+3**Q,b,c]
    elif s=="B":
        DP[i]=[a,a+b,c]
    elif s=="C":
        DP[i]=[a,b,b+c]
    else:
        DP[i]=[3**Q+3*a,a+3*b,b+3*c]
        Q+=1
    for j in range(3):
        DP[i][j]%=mod
print(DP[len(S)-1][2])

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)

Buggy python submission (verdict: Time Limit Exceeded):
mod = 10**9+7
S = input()
Q=0
DP=[[0]*3 for i in range(len(S))]#DP[x][y] x+1文字目までの
#y=0,1,2,3 = A,AB,ABC
if S[0] == "A" or S[0]=="?":
    DP[0] = [1,0,0]
    if S[0]=="?":
        Q=1
else:
    DP[0] = [0,0,0]
for i in range(1,len(S)):
    s = S[i]
    a,b,c = DP[i-1]
    if s=="A":
        DP[i]=[a+3**Q,b,c]
    elif s=="B":
        DP[i]=[a,a+b,c]
    elif s=="C":
        DP[i]=[a,b,b+c]
    else:
        DP[i]=[3**Q+3*a,a+3*b,b+3*c]
        Q+=1
    for j in range(3):
        DP[i][j]%=mod
print(DP[len(S)-1][2])

Fixed submission:
mod = 10**9+7
S = list(input())
Thankyou = 1
DP=[]
#DP[x][y] x+1文字目までを見たときの、
#y=0,1,2 : A,AB,ABCの登場回数
if S[0] == "A" or S[0]=="?":
    DP.append([1,0,0])
    if S[0]=="?":
        Thankyou = 3
else:
    DP.append([0,0,0])
for i,s in enumerate(S):

    if i==0:continue
    a,b,c = DP[i-1]
    if s=="A":
        DP.append([(a+Thankyou)%mod,b,c])
    elif s=="B":
        DP.append([a,(a+b)%mod,c])
    elif s=="C":
        DP.append([a,b,(b+c)%mod])
    else:
        DP.append([(Thankyou+3*a)%mod,(a+3*b)%mod,(b+3*c)%mod])
        Thankyou *= 3
        Thankyou %= mod
print(DP[len(S)-1][2]%mod)