#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <random>
#include <fstream>
#include <string>
#include <tuple>
#include <deque>

#define REP(i, N) for(int i = 0; i< N; i++)
using namespace std;
#define ll long long
const int INF = 1 << 29;
const ll llINF = 1 << 35;

const ll MOD = 1000000007;

ll nasi[100010];
ll a[100010];
ll ab[100010];
ll abc[100010];
ll dp[100010][5];
int weloveabc(){
    char S[100010];
    int slong = 0;
    while((S[slong] = getchar()) != '\n'){
        slong++;
    }
    nasi[0] = 1;
    a[0] = 0;
    ab[0] = 0;
    abc[0] = 0;
    REP(i, slong){
        if(S[i] == '?'){
            nasi[i + 1] = nasi[i] * 3;
            a[i + 1] = a[i] * 3 + nasi[i];
            ab[i + 1] = ab[i] * 3 + a[i];
            abc[i + 1] = abc[i] * 3 + ab[i];
        }else if(S[i] == 'A'){
            nasi[i + 1] = nasi[i];
            a[i + 1] = a[i] + nasi[i];
            ab[i + 1] = ab[i];
            abc[i + 1] = abc[i];
        }else if(S[i] == 'B'){
            nasi[i + 1] = nasi[i];
            a[i + 1] = a[i];
            ab[i + 1] = ab[i] + a[i];
            abc[i + 1] = abc[i];
        }else if(S[i] == 'C'){
            nasi[i + 1] = nasi[i];
            a[i + 1] = a[i];
            ab[i + 1] = ab[i];
            abc[i + 1] = abc[i] + ab[i];
        }
        nasi[i + 1] %= MOD;
        a[i + 1] %= MOD;
        ab[i + 1] %= MOD;
        abc[i + 1] %= MOD;
    }
/*    int m1, m2;
    char ABC[3] = {'A', 'B' ,'C'};
    dp[slong][3] = 1;
    REP(i, slong){
        REP(j, 4){
            if(S[slong - i - 1] == '?'){
                m1 = 3;
            }else{
                m1 = 1;
            }
            if((S[slong - i - 1] == ABC[3 - j] || S[slong - i - 1] == '?') && j != 0){
                m2 = 1;
            }else{
                m2 = 0;
            }
            dp[slong - i - 1][3 - j] = (dp[slong - i][3 - j] * m1 + dp[slong - i][4 - j] * m2) % MOD;
            
        }
    }
    */
    return abc[slong] % MOD;
}
/*
int dp[20][1010];
int p[20];
int c[20];
int allgreen(){
    int D, G;
    cin >> D >> G;
    G /= 100;
    int mondaisuu = 0;
    REP(i, D){
        int cc;
        cin >> p[i] >> cc;
        c[i] = cc / 100;
    }
    
    REP(i, D){
        REP(j, p[i]){
            mondaisuu ++;
            for(int k = mondaisuu; k > j; k --){
                int bonasu = 0;
                if(j + 1 == p[i]){bonasu = c[i];}
                dp[i + 1][k] = max(dp[i][k], dp[i][k - j - 1] + (i + 1) * (j + 1) + bonasu);
            }
        }
    }
    int saishoumondaisuu = 0;
    REP(i, mondaisuu + 1){
        if(dp[D][i] >= G){
            saishoumondaisuu = i;
            break;
        }
    }
    
    return saishoumondaisuu;
}
*/
int main(){
    
    
   /* char S[20];
    int i = 0;
    bool mitasu = true;
    while((S[i] = getchar()) != '\n'){
        i++;
    }
    
    if(S[0] != 'A')mitasu = false;
    bool is_C = false;
    for(int j = 2; j < i - 1; j++){
        if(S[j] == 'C'){
            is_C = true;
            S[j] = 'c';
            break;
        }
    }
    if(!is_C)mitasu = false;
    bool komoji = true;
    for(int j = 1;j < i; j++){
        if(S[j] >= 'a' && S[j] <= 'z'){
        }else{
            komoji = false;
            break;
        }
    }
    if(!komoji)mitasu = false;
    if(mitasu)printf("AC\n");
    else {printf("WA\n");}
    */
    printf("%d\n",weloveabc());
}


Measured memory_bytes: 4480