//https://github.com/kenkoooo/competitive-programming-rs/blob/master/src/math/mod_int.rs
//を参考にしています
mod modint {
    pub const MOD: u32 = 1000000007;
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
    #[derive(Clone, Copy)]
    pub struct ModInt<T: Copy + Clone>(pub T);
    type Num = u32;
    impl Add<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn add(self, other: ModInt<Num>) -> ModInt<Num> {
            let mut d = self.0 + other.0;
            if d >= MOD {
                d -= MOD;
            }
            ModInt(d)
        }
    }
    impl AddAssign<ModInt<Num>> for ModInt<Num> {
        fn add_assign(&mut self, other: ModInt<Num>) {
            *self = *self + other;
        }
    }
    impl Sub<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn sub(self, other: ModInt<Num>) -> ModInt<Num> {
            let mut d = self.0 + MOD - other.0;
            if d >= MOD {
                d -= MOD;
            }
            ModInt(d)
        }
    }
    impl SubAssign<ModInt<Num>> for ModInt<Num> {
        fn sub_assign(&mut self, other: ModInt<Num>) {
            *self = *self - other;
        }
    }
    impl Mul<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn mul(self, other: ModInt<Num>) -> ModInt<Num> {
            ModInt(((self.0 as u64) * (other.0 as u64) % (MOD as u64)) as u32)
        }
    }
    impl MulAssign<ModInt<Num>> for ModInt<Num> {
        fn mul_assign(&mut self, other: ModInt<Num>) {
            *self = *self * other;
        }
    }
    impl Div<Num> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn div(self, other: Num) -> ModInt<Num> {
            self * ModInt(other).pow(MOD - 2)
        }
    }
    impl Div<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn div(self, other: ModInt<Num>) -> ModInt<Num> {
            self * other.pow(MOD - 2)
        }
    }
    impl DivAssign<ModInt<Num>> for ModInt<Num> {
        fn div_assign(&mut self, other: ModInt<Num>) {
            *self = *self / other;
        }
    }
    impl Neg for ModInt<Num> {
        type Output = ModInt<Num>;
        fn neg(self) -> ModInt<Num> {
            ModInt(if self.0 == 0 { 0 } else { MOD - self.0 })
        }
    }
    impl ModInt<Num> {
        pub fn new(v: u32) -> ModInt<Num> {
            ModInt(v % MOD)
        }
        pub fn pow(self, mut n: u32) -> ModInt<Num> {
            let mut t = ModInt::new(1);
            let mut s = self;
            while n > 0 {
                if (n & 1) == 1 {
                    t *= s;
                }
                s *= s;
                n >>= 1;
            }
            t
        }
    }
    use std;
    impl std::fmt::Display for ModInt<Num> {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
}

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

// ここまで

fn run() {
    input! {
        s: chars,
    }
    use modint::*;
    let mut dp = vec![ModInt(0); 4];
    dp[0] = ModInt(1);
    let p = ModInt(3).pow(MOD - 2);
    let mut cnt = 0;
    for c in s {
        match c {
            'A' => dp[1] += dp[0],
            'B' => dp[2] += dp[1],
            'C' => dp[3] += dp[2],
            '?' => {
                cnt += 1;
                for i in (1..4).rev() {
                    dp[i] += p * dp[i - 1];
                }
            },
            _ => (),
        }
    }
    let ans = dp[3] * ModInt(3).pow(cnt);
    println!("{}", ans);
}

fn main() {
    run();
}


Measured memory_bytes: 4352