s=eval(input())



n=len(s)

dc=dict()

dc['A']=0

dc['B']=1

dc['C']=2

dc['?']=3

dp=[[0 for i in range(4)] for j in range(n+1)]

dp[0][0]=1

mo=10**9+7

for i in range(n):

    for t in range(4):

        ch=s[i]

        m = 1 if dc[ch]==t-1 or dc[ch]==3 else 0

        if t!=0:

            dp[i+1][t]+=(dp[i][t-1]*m)%mo

        dp[i+1][t]+=(dp[i][t]*(3 if dc[ch]==3 else 1))%mo



ans=dp[n][3]

print((ans%mo))

mo=10**9+7

s=eval(input())

n=len(s)

dp=[[0]*(1+n) for i in range(3)]

cc=0

for i in range(n):

    c=s[i]

    if c=='A':

        da=1

        db=0

        dc=0

        dd=1

    elif c=='B':

        da=0

        db=1

        dc=0

        dd=1

    elif c=='C':

        da=0

        db=0

        dc=1

        dd=1

    elif c=='?':

        da=1

        db=1

        dc=1

        dd=3

        

    dp[0][i+1]=dp[0][i]*dd+da*pow(3,cc,mo)

    dp[1][i+1]=dp[1][i]*dd+dp[0][i]*db

    dp[2][i+1]=dp[2][i]*dd+dp[1][i]*dc

    dp[0][i+1]%=mo

    dp[1][i+1]%=mo

    dp[2][i+1]%=mo

    if c=='?':

        cc+=1



print((dp[2][n]%mo))
s = eval(input())
n = len(s)
dc = dict()
dc["A"] = 0
dc["B"] = 1
dc["C"] = 2
dc["?"] = 3
dp = [[0 for i in range(4)] for j in range(n + 1)]
dp[0][0] = 1
mo = 10**9 + 7
for i in range(n):
    for t in range(4):
        ch = s[i]
        m = 1 if dc[ch] == t - 1 or dc[ch] == 3 else 0
        if t != 0:
            dp[i + 1][t] += (dp[i][t - 1] * m) % mo
        dp[i + 1][t] += (dp[i][t] * (3 if dc[ch] == 3 else 1)) % mo
ans = dp[n][3]
print((ans % mo))

mo = 10**9 + 7
s = eval(input())
n = len(s)
dp = [[0] * (1 + n) for i in range(3)]
cc = 0
for i in range(n):
    c = s[i]
    if c == "A":
        da = 1
        db = 0
        dc = 0
        dd = 1
    elif c == "B":
        da = 0
        db = 1
        dc = 0
        dd = 1
    elif c == "C":
        da = 0
        db = 0
        dc = 1
        dd = 1
    elif c == "?":
        da = 1
        db = 1
        dc = 1
        dd = 3
    dp[0][i + 1] = dp[0][i] * dd + da * pow(3, cc, mo)
    dp[1][i + 1] = dp[1][i] * dd + dp[0][i] * db
    dp[2][i + 1] = dp[2][i] * dd + dp[1][i] * dc
    dp[0][i + 1] %= mo
    dp[1][i + 1] %= mo
    dp[2][i + 1] %= mo
    if c == "?":
        cc += 1
print((dp[2][n] % mo))

+mo = 10**9 + 7 -dc = dict() -dc["A"] = 0 -dc["B"] = 1 -dc["C"] = 2 -dc["?"] = 3 -dp = [[0 for i in range(4)] for j in range(n + 1)] -dp[0][0] = 1 -mo = 10**9 + 7 +dp = [[0] * (1 + n) for i in range(3)] +cc = 0 -    for t in range(4): -        ch = s[i] -        m = 1 if dc[ch] == t - 1 or dc[ch] == 3 else 0 -        if t != 0: -            dp[i + 1][t] += (dp[i][t - 1] * m) % mo -        dp[i + 1][t] += (dp[i][t] * (3 if dc[ch] == 3 else 1)) % mo -ans = dp[n][3] -print((ans % mo)) +    c = s[i] +    if c == "A": +        da = 1 +        db = 0 +        dc = 0 +        dd = 1 +    elif c == "B": +        da = 0 +        db = 1 +        dc = 0 +        dd = 1 +    elif c == "C": +        da = 0 +        db = 0 +        dc = 1 +        dd = 1 +    elif c == "?": +        da = 1 +        db = 1 +        dc = 1 +        dd = 3 +    dp[0][i + 1] = dp[0][i] * dd + da * pow(3, cc, mo) +    dp[1][i + 1] = dp[1][i] * dd + dp[0][i] * db +    dp[2][i + 1] = dp[2][i] * dd + dp[1][i] * dc +    dp[0][i + 1] %= mo +    dp[1][i + 1] %= mo +    dp[2][i + 1] %= mo +    if c == "?": +        cc += 1 +print((dp[2][n] % mo))