s=eval(input())

l=len(s)

dp=[4*[0]for _ in range(l+1)]

dp[0][0]=1

for i in range(1,l+1):

  si=s[i-1]

  for j in range(4):dp[i][j]=dp[i-1][j]*((si=="?")*2+1)

  if si=="A":dp[i][1]+=dp[i-1][0]

  if si=="B":dp[i][2]+=dp[i-1][1]

  if si=="C":dp[i][3]+=dp[i-1][2]

  if si=="?":

    dp[i][1]+=dp[i-1][0]

    dp[i][2]+=dp[i-1][1]

    dp[i][3]+=dp[i-1][2]

  for j in range(4):dp[i][j]%=10**9+7

print((dp[-1][-1]))
mod=10**9+7

s=eval(input())

n=len(s)

dp=[[0]*4 for _ in range(n+1)]

dp[0][0]=1

for i in range(1,n+1):

    dp[i][0]=dp[i-1][0]*((s[i-1]=="?")*2+1)

    dp[i][1]=dp[i-1][1]*((s[i-1]=="?")*2+1)

    dp[i][2]=dp[i-1][2]*((s[i-1]=="?")*2+1)

    dp[i][3]=dp[i-1][3]*((s[i-1]=="?")*2+1)

    if s[i-1]=="A" or s[i-1]=="?":

        dp[i][1]+=dp[i-1][0]

    if s[i-1]=="B" or s[i-1]=="?":

        dp[i][2]+=dp[i-1][1]

    if s[i-1]=="C" or s[i-1]=="?":

        dp[i][3]+=dp[i-1][2]

    dp[i][0]%=mod

    dp[i][1]%=mod

    dp[i][2]%=mod

    dp[i][3]%=mod

print((dp[n][3]))
s = eval(input())
l = len(s)
dp = [4 * [0] for _ in range(l + 1)]
dp[0][0] = 1
for i in range(1, l + 1):
    si = s[i - 1]
    for j in range(4):
        dp[i][j] = dp[i - 1][j] * ((si == "?") * 2 + 1)
    if si == "A":
        dp[i][1] += dp[i - 1][0]
    if si == "B":
        dp[i][2] += dp[i - 1][1]
    if si == "C":
        dp[i][3] += dp[i - 1][2]
    if si == "?":
        dp[i][1] += dp[i - 1][0]
        dp[i][2] += dp[i - 1][1]
        dp[i][3] += dp[i - 1][2]
    for j in range(4):
        dp[i][j] %= 10**9 + 7
print((dp[-1][-1]))

mod = 10**9 + 7
s = eval(input())
n = len(s)
dp = [[0] * 4 for _ in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
    dp[i][0] = dp[i - 1][0] * ((s[i - 1] == "?") * 2 + 1)
    dp[i][1] = dp[i - 1][1] * ((s[i - 1] == "?") * 2 + 1)
    dp[i][2] = dp[i - 1][2] * ((s[i - 1] == "?") * 2 + 1)
    dp[i][3] = dp[i - 1][3] * ((s[i - 1] == "?") * 2 + 1)
    if s[i - 1] == "A" or s[i - 1] == "?":
        dp[i][1] += dp[i - 1][0]
    if s[i - 1] == "B" or s[i - 1] == "?":
        dp[i][2] += dp[i - 1][1]
    if s[i - 1] == "C" or s[i - 1] == "?":
        dp[i][3] += dp[i - 1][2]
    dp[i][0] %= mod
    dp[i][1] %= mod
    dp[i][2] %= mod
    dp[i][3] %= mod
print((dp[n][3]))

+mod = 10**9 + 7 -l = len(s) -dp = [4 * [0] for _ in range(l + 1)] +n = len(s) +dp = [[0] * 4 for _ in range(n + 1)] -for i in range(1, l + 1): -    si = s[i - 1] -    for j in range(4): -        dp[i][j] = dp[i - 1][j] * ((si == "?") * 2 + 1) -    if si == "A": +for i in range(1, n + 1): +    dp[i][0] = dp[i - 1][0] * ((s[i - 1] == "?") * 2 + 1) +    dp[i][1] = dp[i - 1][1] * ((s[i - 1] == "?") * 2 + 1) +    dp[i][2] = dp[i - 1][2] * ((s[i - 1] == "?") * 2 + 1) +    dp[i][3] = dp[i - 1][3] * ((s[i - 1] == "?") * 2 + 1) +    if s[i - 1] == "A" or s[i - 1] == "?": -    if si == "B": +    if s[i - 1] == "B" or s[i - 1] == "?": -    if si == "C": +    if s[i - 1] == "C" or s[i - 1] == "?": -    if si == "?": -        dp[i][1] += dp[i - 1][0] -        dp[i][2] += dp[i - 1][1] -        dp[i][3] += dp[i - 1][2] -    for j in range(4): -        dp[i][j] %= 10**9 + 7 -print((dp[-1][-1])) +    dp[i][0] %= mod +    dp[i][1] %= mod +    dp[i][2] %= mod +    dp[i][3] %= mod +print((dp[n][3]))