s = eval(input())

count = 0

a, qa, c, qc = 0, 0, 0, 0

def calc(x, qx, y, qy):

    return ((3*x+qx) * (3*y+qy) * pow3q)

#x*y*pow(3,qx+qy)+qx*y*pow(3,qx+qy-1)+x*qy*pow(3,qx+qy-1)+qx*qy*pow(3,qx+qy-2)

for i in range(len(s)):

    if s[i] == 'C':

        c += 1

    elif s[i] == '?':

        qc += 1

    else:

        continue

pow3q = pow(3, qc-3)

for i in range(len(s)):

    if s[i] == 'A':

        a += 1

    elif s[i] == 'C':

        c -= 1

    elif s[i] == 'B':

        count += 3*calc(a, qa, c, qc)

    else:

        qc -= 1

        count += calc(a, qa, c, qc)

        qa += 1

print((int(count) % 1000000007))
s = eval(input())

count = 0

a, c, q = 0, 0, 0

for i in range(len(s)):

    if s[i] == 'C':

        c += 1

    elif s[i] == '?':

        q += 1

    else:

        continue

c = 3 * c + q

p = pow(3, q-3)

for i in range(len(s)):

    if s[i] == 'A':

        a += 3

    elif s[i] == 'C':

        c -= 3

    elif s[i] == 'B':

        count += 3*a*c

    else:

        c -= 1

        count += a*c

        a += 1

print((int(count*p) % 1000000007))
s = eval(input())
count = 0
a, qa, c, qc = 0, 0, 0, 0


def calc(x, qx, y, qy):
    return (3 * x + qx) * (3 * y + qy) * pow3q


# x*y*pow(3,qx+qy)+qx*y*pow(3,qx+qy-1)+x*qy*pow(3,qx+qy-1)+qx*qy*pow(3,qx+qy-2)
for i in range(len(s)):
    if s[i] == "C":
        c += 1
    elif s[i] == "?":
        qc += 1
    else:
        continue
pow3q = pow(3, qc - 3)
for i in range(len(s)):
    if s[i] == "A":
        a += 1
    elif s[i] == "C":
        c -= 1
    elif s[i] == "B":
        count += 3 * calc(a, qa, c, qc)
    else:
        qc -= 1
        count += calc(a, qa, c, qc)
        qa += 1
print((int(count) % 1000000007))

s = eval(input())
count = 0
a, c, q = 0, 0, 0
for i in range(len(s)):
    if s[i] == "C":
        c += 1
    elif s[i] == "?":
        q += 1
    else:
        continue
c = 3 * c + q
p = pow(3, q - 3)
for i in range(len(s)):
    if s[i] == "A":
        a += 3
    elif s[i] == "C":
        c -= 3
    elif s[i] == "B":
        count += 3 * a * c
    else:
        c -= 1
        count += a * c
        a += 1
print((int(count * p) % 1000000007))

-a, qa, c, qc = 0, 0, 0, 0 - - -def calc(x, qx, y, qy): -    return (3 * x + qx) * (3 * y + qy) * pow3q - - -# x*y*pow(3,qx+qy)+qx*y*pow(3,qx+qy-1)+x*qy*pow(3,qx+qy-1)+qx*qy*pow(3,qx+qy-2) +a, c, q = 0, 0, 0 -        qc += 1 +        q += 1 -pow3q = pow(3, qc - 3) +c = 3 * c + q +p = pow(3, q - 3) +        a += 3 +    elif s[i] == "C": +        c -= 3 +    elif s[i] == "B": +        count += 3 * a * c +    else: +        c -= 1 +        count += a * c -    elif s[i] == "C": -        c -= 1 -    elif s[i] == "B": -        count += 3 * calc(a, qa, c, qc) -    else: -        qc -= 1 -        count += calc(a, qa, c, qc) -        qa += 1 -print((int(count) % 1000000007)) +print((int(count * p) % 1000000007))