val MOD = 1000000000+ 7

fun main(args : Array<String>) {
    val S = readLine()!!


    val dpAny = LongArray(S.length + 1)
    val dpA = LongArray(S.length + 1)
    val dpAB = LongArray(S.length + 1)
    val dpABC = LongArray(S.length + 1)

    dpAny.fill(0)
    dpAny[0] = 1
    dpA.fill(0)
    dpAB.fill(0)
    dpABC.fill(0)

    (1..S.length).forEach {i ->
        val s = S[i - 1]
        dpAny[i] = (dpAny[i-1]) % MOD
        if (s == 'A') {
            dpA[i] = (dpA[i - 1] + dpAny[i-1]) % MOD
        } else {
            dpA[i] = dpA[i - 1] % MOD
        }

        if (s == 'B') {
            dpAB[i] = (dpAB[i - 1] + dpA[i - 1]) % MOD
        } else {
            dpAB[i] = dpAB[i - 1] % MOD
        }

        if (s == 'C') {
            dpABC[i] = (dpABC[i - 1] + dpAB[i - 1]) % MOD
        } else {
            dpABC[i] = dpABC[i - 1] % MOD
        }

        if (s == '?') {
            dpAny[i] = (3 * dpAny[i-1]) % MOD
            dpA[i] = (3 * dpA[i-1] + dpAny[i-1]) % MOD
            dpAB[i] = (3 * dpAB[i-1] + dpA[i - 1]) % MOD
            dpABC[i] = (3 * dpABC[i-1] + dpAB[i - 1]) % MOD
        }
    }

    println(dpABC[S.length])
}