fun main(args: Array<String>) {
    val S = readLine()!!
    val l = Array(2) { LongArray(S.length) }
    val r = Array(2) { LongArray(S.length) }

    for (i in l.indices) {
        for (j in l[i].indices) {
            l[i][j] = if (i == 0 && S[j] == 'A' || i == 1 && S[j] == '?') 1 else 0
            if (j > 0) {
                l[i][j] += l[i][j - 1]
            }
        }

        for (j in r[i].indices.reversed()) {
            r[i][j] = if (i == 0 && S[j] == 'C' || i == 1 && S[j] == '?') 1 else 0
            if (j < r[i].size - 1) {
                r[i][j] += r[i][j + 1]
            }
        }
    }

    val Q = l[1].last()
    val MOD = 1e9.toLong() + 7
    var res = ModInt(0L, MOD)

    for (i in 1 .. S.length - 2) {
        if (S[i] == 'A' || S[i] == 'C') {
            continue
        }

        val q = Q - (if (S[i] == 'B') 0 else 1)

        for (j in l.indices) for (k in r.indices) {
            val t = q - j - k

            res += ModInt(l[j][i - 1], MOD) * r[k][i + 1] * ModInt(3L, MOD).pow(t)
        }
    }
    println(res.value)
}


class ModInt (value: Long, val mod: Long) {
    init {
        if (mod <= 0) {
            throw IllegalArgumentException("ModInt cannot be created for non-positive modulus: $mod")
        }
    }

    val value = (value % mod + mod) % mod

    private fun assertSameMod(other: ModInt) {
        if (mod != other.mod) {
            throw IllegalArgumentException(
                "You cannot operate summation, subtraction and multiplication for ModInts with different moduli: $mod, ${other.mod}"
            )
        }
    }

    operator fun unaryMinus() = ModInt(mod - value, mod)

    operator fun plus(other: ModInt): ModInt {
        assertSameMod(other)

        return ModInt((value + other.value) % mod, mod)
    }

    operator fun plus(other: Long) = plus(ModInt(other, mod))

    operator fun minus(other: ModInt): ModInt {
        assertSameMod(other)

        return plus(-other)
    }

    operator fun minus(other: Long) = minus(ModInt(other, mod))

    operator fun times(other: ModInt): ModInt {
        assertSameMod(other)

        return ModInt((value * other.value) % mod, mod)
    }

    operator fun times(other: Long) = times(ModInt(other, mod))

    fun pow(k: Long): ModInt {
        var res = 1L
        var a = value
        var l = k

        while (l > 0) {
            if ((l and 1L) == 1L) {
                res = (res * a) % mod
            }

            a = a * a % mod
            l /= 2L
        }

        return ModInt(res, mod)
    }

    /**
     * Returns an inverse element of the value.
     * To ensure `m * m.inv() = 1`, the modulus should be prime, since it uses Fermat's little theorem.
     */
    fun inv() = pow(mod - 2)

    operator fun div(other: ModInt): ModInt {
        assertSameMod(other)
        if (other.value == 0L) {
            throw ArithmeticException()
        }

        return times(other.inv())
    }

    operator fun div(other: Long) = div(ModInt(other, mod))
}

class ModCombination(val n: Long, val mod: Long) {
    private val fact = Array(n.toInt() + 1) { ModInt(1, mod) }
    private val invfact = Array(n.toInt() + 1) { ModInt(1, mod) }

    init {
        for (i in 2..n.toInt()) {
            fact[i] = fact[i - 1] * i.toLong()
            invfact[i] = fact[i].inv()
        }
    }

    operator fun get(n: Long, m: Long): ModInt {
        if (m > n || n < 0 || m < 0) {
            return ModInt(0, mod)
        }

        return fact[n.toInt()] * invfact[m.toInt()] * invfact[(n - m).toInt()]
    }
}


Measured memory_bytes: 53436