#include <bits/stdc++.h>
    using namespace std;
    
    long long binpow(long long a, long long b, long long m) {
        a %= m;
        long long res = 1;
        while (b > 0) {
        if (b & 1)
            res = res * a % m;
            a = a * a % m;
            b >>= 1;
        }
        return res;
    }
    
    
    int main(){
      string s;
      cin >> s;
      long len;
      len = s.length();
      array<long, 100000> ar_bc, ar_bq, ar_qc, ar_qq ,ar_c , ar_q ;
      long long c , q; 
      c = count(s.cbegin(), s.cend(), 'C');
      q = count(s.cbegin(), s.cend(), '?'); 
      long long ct , qt;
      ct = c;
      qt = q;
      // ar_cはi番目より右にあるcの数
      for(long i = 0 ; i < len ; i++){
          if(s[i] == 'C'){
              ct -= 1;
          }else if(s[i] == '?'){
              qt -= 1;
          }
          ar_c[i] = ct;
          ar_q[i] = qt;
      }
      //ar_bcはi番目を含んだbcの組み合わせの数＝i番目がbのとき累積するar_cの累積
      //ar_qcはi番目を含んだqcの組み合わせの数＝i番目がqのとき累積するar_cの累積
      //ar_bqはi番目を含んだbqの組み合わせの数＝i番目がbのとき累積するar_qの累積
      //ar_qqはi番目を含んだqqの組み合わせの数＝i番目がqのとき累積するar_qの累積
      ar_bc[len - 1] = 0;
      ar_bq[len - 1] = 0;
      ar_qc[len - 1] = 0;
      ar_qq[len - 1] = 0;
      for(int i = len - 2; i >= 0 ; i--){
          if(s[i] == 'B'){
              ar_bc[i] = ar_bc[i+1] + ar_c[i];
              ar_qc[i] = ar_qc[i+1];
              ar_bq[i] = ar_bq[i+1] + ar_q[i];
              ar_qq[i] = ar_qq[i+1];
          }else if(s[i] == '?'){
              ar_bc[i] = ar_bc[i+1] ;
              ar_qc[i] = ar_qc[i+1] + ar_c[i];
              ar_bq[i] = ar_bq[i+1];
              ar_qq[i] = ar_qq[i+1] + ar_q[i];
          }else{
              ar_bc[i] = ar_bc[i+1];
              ar_qc[i] = ar_qc[i+1];
              ar_bq[i] = ar_bq[i+1];
              ar_qq[i] = ar_qq[i+1];
          }
      }
      /* cout << "ar_bc[i]" << endl;
      for(long i = 0; i < len; i++){
          cout << ar_bc[i] << endl;
      }*/
      /* cout << "ar_c[i]" << endl;
      for(long i = 0; i < len; i++){
          cout << ar_c[i] << endl;
      } */
      /* cout << "ar_qc[i]" << endl;
      for(long i = 0; i < len; i++){
          cout << ar_qc[i] << endl;
      }
      cout << "ar_bq[i]" << endl;
      for(long i = 0; i < len; i++){
          cout << ar_bq[i] << endl;
      }
      cout << "ar_qq[i]" << endl;
      for(long i = 0; i < len; i++){
          cout << ar_qq[i] << endl;
      }  */
      
      int qc1 , qc2;
      long long ans = 0;
      long long q0, q1 , q2, q3;
      long long div = 1000000007;
      q0 = binpow(3 , q , div);
      if(q > 0 ){
          q1 = binpow(3 , q -1 , div);
      }else{
          q1 = 0;
      }
      if(q > 1 ){
          q2 = binpow(3 , q -2 , div);
      }else{
          q2 = 0;
      }
      if(q > 2 ){
          q3 = binpow(3 , q -3 , div);
      }else{
          q3 = 0;
      }
     
      for(int i = 0 ; i < len - 2 ; i++){
        if(s[i] == 'A'){
            ans += ar_bc[i+1] * q0;
            ans += ar_bq[i+1] * q1;
            ans += ar_qc[i+1] * q1;
            ans += ar_qq[i+1] * q2;
        }else if(s[i] == '?'){
            ans += ar_bc[i+1] * q1;
            ans += ar_bq[i+1] * q2;
            ans += ar_qc[i+1] * q2;
            ans += ar_qq[i+1] * q3;
        }
        ans = ans % div;
      } 
      
      cout << ans << endl;
      
      return 0;
    }

Measured memory_bytes: 5120