def main():

    S=eval(input())

    N=len(S)

    abccounter=0

    abcounter=0

    acounter=0

    for i in range(0,N):

        if S[i]=='A':

            acounter+=1

        elif S[i]=='B':

            abcounter+=acounter

        elif S[i]=='C':

            abccounter+=abcounter

    hbccounter=0

    hbcounter=0

    hcounter=0

    for i in range(0,N):

        if S[i]=='?':

            hcounter+=1

        elif S[i]=='B':

            hbcounter+=hcounter

        elif S[i]=='C':

            hbccounter+=hbcounter

    ahccounter=0

    ahcounter=0

    acounter=0

    for i in range(0,N):

        if S[i]=='A':

            acounter+=1

        elif S[i]=='?':

            ahcounter+=acounter

        elif S[i]=='C':

            ahccounter+=ahcounter

    abhcounter=0

    abcounter=0

    acounter=0

    for i in range(0,N):

        if S[i]=='A':

            acounter+=1

        elif S[i]=='B':

            abcounter+=acounter

        elif S[i]=='?':

            abhcounter+=abcounter

    ahhcounter=0

    ahcounter=0

    acounter=0

    for i in range(0,N):

        if S[i]=='A':

            acounter+=1

        elif S[i]=='?':

            ahhcounter+=ahcounter

            ahcounter+=acounter

    hbhcounter=0

    hbcounter=0

    hcounter=0

    for i in range(0,N):

        if S[i]=='B':

            hbcounter+=hcounter

        elif S[i]=='?':

            hbhcounter+=hbcounter

            hcounter+=1

    hhccounter=0

    hhcounter=0

    hcounter=0

    for i in range(0,N):

        if S[i]=='C':

            hhccounter+=hhcounter

        elif S[i]=='?':

            hhcounter+=hcounter

            hcounter+=1

    Q=hcounter

    if Q>=3:

        ans=abccounter*3**Q+(hbccounter+ahccounter+abhcounter)*3**(Q-1)+(ahhcounter+hbhcounter+hhccounter)*3**(Q-2)+(Q*(Q-1)*(Q-2)//6)*3**(Q-3)

    elif Q==2:

        ans=abccounter*3**Q+(hbccounter+ahccounter+abhcounter)*3**(Q-1)+(ahhcounter+hbhcounter+hhccounter)*3**(Q-2)

    elif Q==1:

        ans=abccounter*3**Q+(hbccounter+ahccounter+abhcounter)*3**(Q-1)

    else:

        ans=abccounter*3**Q

    print((ans%(10**9+7)))

if __name__ == '__main__':

    main()

s = input().lower()

n = len(s)



dp_a = 0

dp_ab = 0

dp_abc = 0

hatena = 1



mod = 10**9 + 7



for i in range(n):

    if s[i]=="a":

        dp_a = (dp_a + hatena) % mod

    elif s[i]=="b":

        dp_ab = (dp_a + dp_ab) % mod

    elif s[i]=="c":

        dp_abc = (dp_ab + dp_abc) % mod

    else:

        dp_abc = (dp_ab + 3 * dp_abc) % mod

        dp_ab = (3 * dp_ab + dp_a) % mod

        dp_a = (3 * dp_a + hatena) % mod

        hatena = (hatena * 3) % mod



print(dp_abc)

def main():
    S = eval(input())
    N = len(S)
    abccounter = 0
    abcounter = 0
    acounter = 0
    for i in range(0, N):
        if S[i] == "A":
            acounter += 1
        elif S[i] == "B":
            abcounter += acounter
        elif S[i] == "C":
            abccounter += abcounter
    hbccounter = 0
    hbcounter = 0
    hcounter = 0
    for i in range(0, N):
        if S[i] == "?":
            hcounter += 1
        elif S[i] == "B":
            hbcounter += hcounter
        elif S[i] == "C":
            hbccounter += hbcounter
    ahccounter = 0
    ahcounter = 0
    acounter = 0
    for i in range(0, N):
        if S[i] == "A":
            acounter += 1
        elif S[i] == "?":
            ahcounter += acounter
        elif S[i] == "C":
            ahccounter += ahcounter
    abhcounter = 0
    abcounter = 0
    acounter = 0
    for i in range(0, N):
        if S[i] == "A":
            acounter += 1
        elif S[i] == "B":
            abcounter += acounter
        elif S[i] == "?":
            abhcounter += abcounter
    ahhcounter = 0
    ahcounter = 0
    acounter = 0
    for i in range(0, N):
        if S[i] == "A":
            acounter += 1
        elif S[i] == "?":
            ahhcounter += ahcounter
            ahcounter += acounter
    hbhcounter = 0
    hbcounter = 0
    hcounter = 0
    for i in range(0, N):
        if S[i] == "B":
            hbcounter += hcounter
        elif S[i] == "?":
            hbhcounter += hbcounter
            hcounter += 1
    hhccounter = 0
    hhcounter = 0
    hcounter = 0
    for i in range(0, N):
        if S[i] == "C":
            hhccounter += hhcounter
        elif S[i] == "?":
            hhcounter += hcounter
            hcounter += 1
    Q = hcounter
    if Q >= 3:
        ans = (
            abccounter * 3**Q
            + (hbccounter + ahccounter + abhcounter) * 3 ** (Q - 1)
            + (ahhcounter + hbhcounter + hhccounter) * 3 ** (Q - 2)
            + (Q * (Q - 1) * (Q - 2) // 6) * 3 ** (Q - 3)
        )
    elif Q == 2:
        ans = (
            abccounter * 3**Q
            + (hbccounter + ahccounter + abhcounter) * 3 ** (Q - 1)
            + (ahhcounter + hbhcounter + hhccounter) * 3 ** (Q - 2)
        )
    elif Q == 1:
        ans = abccounter * 3**Q + (hbccounter + ahccounter + abhcounter) * 3 ** (
            Q - 1
        )
    else:
        ans = abccounter * 3**Q
    print((ans % (10**9 + 7)))


if __name__ == "__main__":
    main()

s = input().lower()
n = len(s)
dp_a = 0
dp_ab = 0
dp_abc = 0
hatena = 1
mod = 10**9 + 7
for i in range(n):
    if s[i] == "a":
        dp_a = (dp_a + hatena) % mod
    elif s[i] == "b":
        dp_ab = (dp_a + dp_ab) % mod
    elif s[i] == "c":
        dp_abc = (dp_ab + dp_abc) % mod
    else:
        dp_abc = (dp_ab + 3 * dp_abc) % mod
        dp_ab = (3 * dp_ab + dp_a) % mod
        dp_a = (3 * dp_a + hatena) % mod
        hatena = (hatena * 3) % mod
print(dp_abc)

-def main(): -    S = eval(input()) -    N = len(S) -    abccounter = 0 -    abcounter = 0 -    acounter = 0 -    for i in range(0, N): -        if S[i] == "A": -            acounter += 1 -        elif S[i] == "B": -            abcounter += acounter -        elif S[i] == "C": -            abccounter += abcounter -    hbccounter = 0 -    hbcounter = 0 -    hcounter = 0 -    for i in range(0, N): -        if S[i] == "?": -            hcounter += 1 -        elif S[i] == "B": -            hbcounter += hcounter -        elif S[i] == "C": -            hbccounter += hbcounter -    ahccounter = 0 -    ahcounter = 0 -    acounter = 0 -    for i in range(0, N): -        if S[i] == "A": -            acounter += 1 -        elif S[i] == "?": -            ahcounter += acounter -        elif S[i] == "C": -            ahccounter += ahcounter -    abhcounter = 0 -    abcounter = 0 -    acounter = 0 -    for i in range(0, N): -        if S[i] == "A": -            acounter += 1 -        elif S[i] == "B": -            abcounter += acounter -        elif S[i] == "?": -            abhcounter += abcounter -    ahhcounter = 0 -    ahcounter = 0 -    acounter = 0 -    for i in range(0, N): -        if S[i] == "A": -            acounter += 1 -        elif S[i] == "?": -            ahhcounter += ahcounter -            ahcounter += acounter -    hbhcounter = 0 -    hbcounter = 0 -    hcounter = 0 -    for i in range(0, N): -        if S[i] == "B": -            hbcounter += hcounter -        elif S[i] == "?": -            hbhcounter += hbcounter -            hcounter += 1 -    hhccounter = 0 -    hhcounter = 0 -    hcounter = 0 -    for i in range(0, N): -        if S[i] == "C": -            hhccounter += hhcounter -        elif S[i] == "?": -            hhcounter += hcounter -            hcounter += 1 -    Q = hcounter -    if Q >= 3: -        ans = ( -            abccounter * 3**Q -            + (hbccounter + ahccounter + abhcounter) * 3 ** (Q - 1) -            + (ahhcounter + hbhcounter + hhccounter) * 3 ** (Q - 2) -            + (Q * (Q - 1) * (Q - 2) // 6) * 3 ** (Q - 3) -        ) -    elif Q == 2: -        ans = ( -            abccounter * 3**Q -            + (hbccounter + ahccounter + abhcounter) * 3 ** (Q - 1) -            + (ahhcounter + hbhcounter + hhccounter) * 3 ** (Q - 2) -        ) -    elif Q == 1: -        ans = abccounter * 3**Q + (hbccounter + ahccounter + abhcounter) * 3 ** ( -            Q - 1 -        ) +s = input().lower() +n = len(s) +dp_a = 0 +dp_ab = 0 +dp_abc = 0 +hatena = 1 +mod = 10**9 + 7 +for i in range(n): +    if s[i] == "a": +        dp_a = (dp_a + hatena) % mod +    elif s[i] == "b": +        dp_ab = (dp_a + dp_ab) % mod +    elif s[i] == "c": +        dp_abc = (dp_ab + dp_abc) % mod -        ans = abccounter * 3**Q -    print((ans % (10**9 + 7))) - - -if __name__ == "__main__": -    main() +        dp_abc = (dp_ab + 3 * dp_abc) % mod +        dp_ab = (3 * dp_ab + dp_a) % mod +        dp_a = (3 * dp_a + hatena) % mod +        hatena = (hatena * 3) % mod +print(dp_abc)