S = eval(input())



A_left=[0]

C_left=[0]

Q_left=[0]



for (i,s) in enumerate(S):

    if s == "A":

        A_left.append(A_left[i]+1)

        C_left.append(C_left[i])

        Q_left.append(Q_left[i])

    elif s == "C":

        A_left.append(A_left[i])

        C_left.append(C_left[i]+1)

        Q_left.append(Q_left[i])

    elif s == "?":

        A_left.append(A_left[i])

        C_left.append(C_left[i])

        Q_left.append(Q_left[i]+1)

    else:

        A_left.append(A_left[i])

        C_left.append(C_left[i])

        Q_left.append(Q_left[i])



A_right = []

C_right = []

Q_right = []

for i in range(len(S)):

    A_right.append(A_left[-1] - A_left[i+1])

    C_right.append(C_left[-1] - C_left[i+1])

    Q_right.append(Q_left[-1] - Q_left[i+1])



ans = 0

for (i,s) in enumerate(S):

    if s == "B":

        ans = (ans + A_left[i] * C_right[i] * pow(3,Q_left[-1],10**9+7)) % (10**9+7)

        if Q_left[-1]>=1:

            ans = (ans + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i]) * pow(3,(Q_left[-1]-1),10**9+7)) % (10**9+7)

        if Q_left[-1]>=2:

            ans = (ans + (Q_left[i] * Q_right[i]) * pow(3,(Q_left[-1]-2),10**9+7)) % (10**9+7)

    elif s == "?":

        if Q_left[-1]>=1:

            ans = (ans + A_left[i] * C_right[i] * pow(3,(Q_left[-1]-1),10**9+7)) % (10**9+7)

        if Q_left[-1]>=2:

            ans = (ans + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i]) * pow(3,(Q_left[-1]-2),10**9+7)) % (10**9+7)

        if Q_left[-1]>=3:

            ans = (ans + (Q_left[i] * Q_right[i]) * pow(3,(Q_left[-1]-3),10**9+7)) % (10**9+7)



print(ans)

box = [1,0,0,0]

for c in eval(input()):

    if c == "A":

        box[1] = box[0]+box[1]

    elif c == "B":

        box[2] = box[1]+box[2]

    elif c == "C":

        box[3] = box[2]+box[3] 

    elif c == "?":

        box = [box[0]*3,box[1]*3+box[0],box[2]*3+box[1],box[3]*3+box[2]]



    box = [i%(10**9+7) for i in box]

print((box[3]))
S = eval(input())
A_left = [0]
C_left = [0]
Q_left = [0]
for (i, s) in enumerate(S):
    if s == "A":
        A_left.append(A_left[i] + 1)
        C_left.append(C_left[i])
        Q_left.append(Q_left[i])
    elif s == "C":
        A_left.append(A_left[i])
        C_left.append(C_left[i] + 1)
        Q_left.append(Q_left[i])
    elif s == "?":
        A_left.append(A_left[i])
        C_left.append(C_left[i])
        Q_left.append(Q_left[i] + 1)
    else:
        A_left.append(A_left[i])
        C_left.append(C_left[i])
        Q_left.append(Q_left[i])
A_right = []
C_right = []
Q_right = []
for i in range(len(S)):
    A_right.append(A_left[-1] - A_left[i + 1])
    C_right.append(C_left[-1] - C_left[i + 1])
    Q_right.append(Q_left[-1] - Q_left[i + 1])
ans = 0
for (i, s) in enumerate(S):
    if s == "B":
        ans = (ans + A_left[i] * C_right[i] * pow(3, Q_left[-1], 10**9 + 7)) % (
            10**9 + 7
        )
        if Q_left[-1] >= 1:
            ans = (
                ans
                + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i])
                * pow(3, (Q_left[-1] - 1), 10**9 + 7)
            ) % (10**9 + 7)
        if Q_left[-1] >= 2:
            ans = (
                ans + (Q_left[i] * Q_right[i]) * pow(3, (Q_left[-1] - 2), 10**9 + 7)
            ) % (10**9 + 7)
    elif s == "?":
        if Q_left[-1] >= 1:
            ans = (
                ans + A_left[i] * C_right[i] * pow(3, (Q_left[-1] - 1), 10**9 + 7)
            ) % (10**9 + 7)
        if Q_left[-1] >= 2:
            ans = (
                ans
                + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i])
                * pow(3, (Q_left[-1] - 2), 10**9 + 7)
            ) % (10**9 + 7)
        if Q_left[-1] >= 3:
            ans = (
                ans + (Q_left[i] * Q_right[i]) * pow(3, (Q_left[-1] - 3), 10**9 + 7)
            ) % (10**9 + 7)
print(ans)

box = [1, 0, 0, 0]
for c in eval(input()):
    if c == "A":
        box[1] = box[0] + box[1]
    elif c == "B":
        box[2] = box[1] + box[2]
    elif c == "C":
        box[3] = box[2] + box[3]
    elif c == "?":
        box = [
            box[0] * 3,
            box[1] * 3 + box[0],
            box[2] * 3 + box[1],
            box[3] * 3 + box[2],
        ]
    box = [i % (10**9 + 7) for i in box]
print((box[3]))

-S = eval(input()) -A_left = [0] -C_left = [0] -Q_left = [0] -for (i, s) in enumerate(S): -    if s == "A": -        A_left.append(A_left[i] + 1) -        C_left.append(C_left[i]) -        Q_left.append(Q_left[i]) -    elif s == "C": -        A_left.append(A_left[i]) -        C_left.append(C_left[i] + 1) -        Q_left.append(Q_left[i]) -    elif s == "?": -        A_left.append(A_left[i]) -        C_left.append(C_left[i]) -        Q_left.append(Q_left[i] + 1) -    else: -        A_left.append(A_left[i]) -        C_left.append(C_left[i]) -        Q_left.append(Q_left[i]) -A_right = [] -C_right = [] -Q_right = [] -for i in range(len(S)): -    A_right.append(A_left[-1] - A_left[i + 1]) -    C_right.append(C_left[-1] - C_left[i + 1]) -    Q_right.append(Q_left[-1] - Q_left[i + 1]) -ans = 0 -for (i, s) in enumerate(S): -    if s == "B": -        ans = (ans + A_left[i] * C_right[i] * pow(3, Q_left[-1], 10**9 + 7)) % ( -            10**9 + 7 -        ) -        if Q_left[-1] >= 1: -            ans = ( -                ans -                + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i]) -                * pow(3, (Q_left[-1] - 1), 10**9 + 7) -            ) % (10**9 + 7) -        if Q_left[-1] >= 2: -            ans = ( -                ans + (Q_left[i] * Q_right[i]) * pow(3, (Q_left[-1] - 2), 10**9 + 7) -            ) % (10**9 + 7) -    elif s == "?": -        if Q_left[-1] >= 1: -            ans = ( -                ans + A_left[i] * C_right[i] * pow(3, (Q_left[-1] - 1), 10**9 + 7) -            ) % (10**9 + 7) -        if Q_left[-1] >= 2: -            ans = ( -                ans -                + (Q_left[i] * C_right[i] + A_left[i] * Q_right[i]) -                * pow(3, (Q_left[-1] - 2), 10**9 + 7) -            ) % (10**9 + 7) -        if Q_left[-1] >= 3: -            ans = ( -                ans + (Q_left[i] * Q_right[i]) * pow(3, (Q_left[-1] - 3), 10**9 + 7) -            ) % (10**9 + 7) -print(ans) +box = [1, 0, 0, 0] +for c in eval(input()): +    if c == "A": +        box[1] = box[0] + box[1] +    elif c == "B": +        box[2] = box[1] + box[2] +    elif c == "C": +        box[3] = box[2] + box[3] +    elif c == "?": +        box = [ +            box[0] * 3, +            box[1] * 3 + box[0], +            box[2] * 3 + box[1], +            box[3] * 3 + box[2], +        ] +    box = [i % (10**9 + 7) for i in box] +print((box[3]))