s=eval(input())

n=len(s)

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

dp[0][0]=1

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

  for j in range(4):

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

  if s[i-1] in "A?":dp[i][1]+=dp[i-1][0]

  if s[i-1] in "B?":dp[i][2]+=dp[i-1][1]

  if s[i-1] in "C?":dp[i][3]+=dp[i-1][2]

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

#for i in range(n+1):print(dp[i],(" "+s)[i])

print((dp[-1][-1]))
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]))
s = eval(input())
n = len(s)
dp = [4 * [0] for i in range(n + 1)]
dp[0][0] = 1
for i in range(1, n + 1):
    for j in range(4):
        dp[i][j] += dp[i - 1][j] * ((s[i - 1] == "?") * 2 + 1)
    if s[i - 1] in "A?":
        dp[i][1] += dp[i - 1][0]
    if s[i - 1] in "B?":
        dp[i][2] += dp[i - 1][1]
    if s[i - 1] in "C?":
        dp[i][3] += dp[i - 1][2]
    for j in range(4):
        dp[i][j] %= 10**9 + 7
# for i in range(n+1):print(dp[i],(" "+s)[i])
print((dp[-1][-1]))

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]))

-n = len(s) -dp = [4 * [0] for i in range(n + 1)] +l = len(s) +dp = [4 * [0] for _ in range(l + 1)] -for i in range(1, n + 1): +for i in range(1, l + 1): +    si = s[i - 1] -        dp[i][j] += dp[i - 1][j] * ((s[i - 1] == "?") * 2 + 1) -    if s[i - 1] in "A?": +        dp[i][j] = dp[i - 1][j] * ((si == "?") * 2 + 1) +    if si == "A": -    if s[i - 1] in "B?": +    if si == "B": -    if s[i - 1] in "C?": +    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] -# for i in range(n+1):print(dp[i],(" "+s)[i])