#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */
string solveA(){
    ll R; cin >> R;
    return R < 1200 ? "ABC": R < 2800?"ARC":"AGC";
}
string solveB(){
    string S; cin >> S;
    vector<int> flag(S.length());
    if(*S.begin() != 'A') return "WA";
    bool a = false;
    for(int i = 2; i < S.length()-1;i++){
        if(S[i] == 'C'){
            if(a) return "WA";
            a = true;
            flag[i] = true;
        }
    }
    flag[0] = true;
    if(!a) return "WA";
    
    for(int i = 0; i < S.length();i++){
        if(flag[i])continue;
        if(islower(S[i])) continue;
        return "WA";
    }
    return "AC";
}

ll solveC(){
    ll res = LINF;
    int D,G; cin >> D >> G;
    vector<ll> p(D),c(D);
    for(int i = 0; i < D;i++) cin >> p[i] >> c[i];
    
    G/= 100;
    for(auto& v:c) v/=100;
    
    for(int i = 0; i < (1<<D);i++){
        ll L = G;
        ll cnt = 0;
        for(int j = 0; j < D;j++){
            if((i>>j)&1){
                L -= (j+1)*p[j]+c[j];
                cnt += p[j];
            }
        }
        if(L <= 0){
            res = min(res,cnt);
            continue;
        }
        for(int j = D-1;j>=0;j--){
            if((i>>j)&1) continue;
            ll num = min(p[j],(L+j)/(j+1));
            L -= num*(j+1);
            cnt += num;
            if(L <= 0) break;
        }
        if(L <= 0) res = min(res,cnt);
    }
    return res;
}

const ll MAX_N = 2e5 + 10;
const ll MOD = 1e9+7;
ll Inv[MAX_N];
ll fact[MAX_N];
ll factInv[MAX_N];
void Inv_init(){
    Inv[1] = 1;
    for(int i = 2; i < MAX_N; i++) {
        Inv[i] = Inv[MOD%i] * (MOD - MOD/i) % MOD;
    }
}
void fact_init(){
    fact[0] = fact[1] = factInv[0] = factInv[1] = 1;
    for(int i = 2; i < MAX_N; i++){
        fact[i] = (fact[i-1] * i) % MOD;
        factInv[i] = (factInv[i-1] * Inv[i])%MOD;
    }
}

// a^b % MOD;
ll powmod(ll a,ll b) {ll res=1;a%=MOD;if(b<0)return res; for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// 逆元 1/a % MOD
ll inversemod(ll a) { return powmod(a,MOD - 2);}
ll nCr(int n,int r) {
    if(n < r) return 0;
    return (fact[n] * (factInv[r] * factInv[n-r] % MOD)) % MOD;
}

void init(){
    Inv_init();
    fact_init();
}

ll solveD(){
    init();
    ll res = 0;
    string S; cin >> S;
    replace(S.begin(),S.end(),'?','D');
    ll N = S.length();
    vector<vector<ll>> cusum(N+1,vector<ll>(4));
    for(int i = 0; i < N;i++){
        for(int j = 0; j < 4;j++){
            cusum[i+1][j] = cusum[i][j];
        }
        cusum[i+1][S[i]-'A']++;
    }

    for(int i = 0; i < N;i++){
        if(S[i] == 'B'){
            (res += cusum[i][0]*(cusum[N][2]-cusum[i+1][2])%MOD*powmod(3,cusum[N][3])%MOD)%=MOD;
            (res += cusum[i][3]*(cusum[N][2]-cusum[i+1][2])%MOD*powmod(3,cusum[N][3]-1)%MOD)%=MOD;
            (res += cusum[i][0]*(cusum[N][3]-cusum[i+1][3])%MOD*powmod(3,cusum[N][3]-1)%MOD)%=MOD;
            (res += cusum[i][3]*(cusum[N][3]-cusum[i+1][3])%MOD*powmod(3,cusum[N][3]-2)%MOD)%=MOD;
        }
        if(S[i] == 'D'){
            (res += cusum[i][0]*(cusum[N][2]-cusum[i+1][2])%MOD*powmod(3,cusum[N][3]-1)%MOD)%=MOD;
            (res += cusum[i][3]*(cusum[N][2]-cusum[i+1][2])%MOD*powmod(3,cusum[N][3]-2)%MOD)%=MOD;
            (res += cusum[i][0]*(cusum[N][3]-cusum[i+1][3])%MOD*powmod(3,cusum[N][3]-2)%MOD)%=MOD;
            (res += cusum[i][3]*(cusum[N][3]-cusum[i+1][3])%MOD*powmod(3,cusum[N][3]-3)%MOD)%=MOD;
        }
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    //cout << solveA() << endl;
    //cout << solveB() << endl;
    //cout << solveC() << endl;
    cout << solveD() << endl;
    return 0;
}


Measured memory_bytes: 12112