import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

const mod = 10^^9 + 7;

long calc(string s) {
    auto dp = new long[][](s.length + 1, 4);
    dp[0][0] = 1;

    for (int i = 1; i <= s.length; i++) {
        long factor = s[i-1] == '?' ? 3 : 1;
        for (int j = 0; j <= 3; j++) {
            dp[i][j] = (dp[i-1][j] * factor) % mod;
        }

        auto c = s[i-1];
        if (c == 'A' || c == '?')
            dp[i][1] = (dp[i][1] + dp[i-1][0]) % mod;
        if (c == 'B' || c == '?')
            dp[i][2] = (dp[i][2] + dp[i-1][1]) % mod;
        if (c == 'C' || c == '?')
            dp[i][3] = (dp[i][3] + dp[i-1][2]) % mod;
    }

    return dp[s.length][3];
}

void main() {
    string s = read!string;
    writeln(calc(s));
}
