import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;

void main()
{
    auto s = readln.chomp;
    auto abc = "ABC";
    auto mod = 10 ^^ 9 + 7;

    long[][4] dp;
    foreach (ref row; dp)
        row = new long[](s.length + 1);

    dp[3][$ - 1] = 1;

    foreach_reverse (i; 0 .. s.length)
        if (s[i] == '?')
            dp[3][i] = dp[3][i + 1] * 3 % mod;
        else
            dp[3][i] = dp[3][i + 1];

    foreach_reverse (i; 0 .. s.length)
        foreach (j; 0 .. 3)
        {
            if (s[i] == '?')
                dp[j][i] = dp[j][i + 1] * 3 % mod;
            else
                dp[j][i] = dp[j][i + 1];
            if (s[i] == '?' || s[i] == abc[j])
                dp[j][i] = (dp[j][i] + dp[j + 1][i + 1]) % mod;
        }

    // foreach (r; dp)
    //     writeln(r);

    dp[0][0].writeln;
}
