#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

// ok return 0
fn mod_pow(a: i64, n: i64, MOD: i64) -> i64 {
    if n<0 {
        return 0;
    }
    let mut res: i64 = 1;
    let mut m = n;
    let mut b = a;
    loop {
        // println!("{} {}", m, res);
        if m==0 {
            break;
        }
        if m%2 == 1 {
            res *= b;
            res%=MOD;
        }
        b = b*b;
        b%=MOD;
        m >>= 1;
    }
    res%MOD
}
fn main() {
    input!{
        s: chars
    }
    let n = s.len();
    let MOD:i64 = 1_000_000_007;
    let mut a_left:Vec<i64> = vec![0;n];
    let mut c_right:Vec<i64> = vec![0;n];
    let mut q_left:Vec<i64> = vec![0;n];
    let mut q_right:Vec<i64> = vec![0;n];
    for i in 0..n {
        if s[i]=='A' {
            if i != n-1 {
                a_left[i+1] += 1;
            }
        }
        if s[i]=='C' {
            if i != 0 {
                c_right[i-1] += 1;
            }
        }
        if s[i]=='?' {
            if i!=n-1 {
                q_left[i+1] += 1;
            }
            if i!=0 {
                q_right[i-1] += 1;
            }
        }
    }
    for i in 0..n-1 {
        a_left[i+1] += a_left[i];
        q_left[i+1] += q_left[i];
    }
    for i in (0..n-1).rev() {
        c_right[i] += c_right[i+1];
        q_right[i] += q_right[i+1];
    }
    let mut ans:i64 = 0;
    // println!("{:?} {:?} {:?} {:?}", a_left, c_right, q_left, q_right);
    for i in 0..n {
        let m = (q_left[i] + q_right[i]) as u64;
        if s[i]=='B' {
            ans += a_left[i]*c_right[i]%MOD*mod_pow(3, m as i64, MOD)%MOD;
            ans%=MOD;
            ans += q_left[i]*c_right[i]%MOD*mod_pow(3, m as i64-1, MOD)%MOD;
            ans%=MOD;
            ans += a_left[i]*q_right[i]%MOD*mod_pow(3, m as i64-1, MOD)%MOD;
            ans%=MOD;
            ans += q_left[i]*q_right[i]%MOD*mod_pow(3, m as i64-2, MOD)%MOD;
            ans%=MOD;
        }
        if s[i]=='?' {
            ans += a_left[i]*c_right[i]%MOD*mod_pow(3, m as i64, MOD)%MOD;
            ans%=MOD;
            ans += q_left[i]*c_right[i]%MOD*mod_pow(3, m as i64-1, MOD)%MOD;
            ans%=MOD;
            ans += a_left[i]*q_right[i]%MOD*mod_pow(3, m as i64-1, MOD)%MOD;
            ans%=MOD;
            ans += q_left[i]*q_right[i]%MOD*mod_pow(3, m as i64-2, MOD)%MOD;
            ans%=MOD;
        }
    }
    println!("{}", ans);
    // println!("{}", mod_pow(2, 10, MOD));
}

Measured memory_bytes: 8444