#include "bits/stdc++.h"
#define Rep(i,n) for(int i=0;i<n;i++)
#define For(i,n1,n2) for(int i=n1;i<n2;i++)
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define put(a) cout<<a<<endl;
#define all(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define TDARRAY(int,a,n,m) vector<vector<int>> a(n,vector<int>(m,0));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) {if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a, T b) {if(a>b){a=b;return 1;}return 0;}

class Modulo{
public:
    ll val;
    static const ll mod = 1e9+7;
    vector<ll> factable = {1};
    vector<ll> invtable = {1};
    ll pro(ll x,ll y){return ((x%mod)*(y%mod))%mod;}
    ll sum(ll x,ll y){return ((x%mod)+(y%mod))%mod;}
    ll dif(ll x,ll y){ll d =((x%mod)-(y%mod))%mod;if(d>=0){return d;}else{return d+mod;}}
    ll quo(ll x, ll y){return pro(x,pow(y,mod-2));}
    ll pow(ll x,ll y){
        if(y<=0){return 1;}
        if(y%2==0){ll d = pow(x,y/2);return ((d%mod)*(d%mod))%mod;}
        else{return (x*pow(x,y-1))%mod;}
    }
    void operator=(ll n){this->val = n%mod;}
    ll operator+(ll n){return sum(this->val,n);}
    ll operator-(ll n){return dif(this->val,n);}
    ll operator*(ll n){return pro(this->val,n);}
    ll operator/(ll n){return quo(this->val,n);}
    void operator+=(ll n){this->val = sum(this->val,n);}
    void operator-=(ll n){this->val = dif(this->val,n);}
    void operator*=(ll n){this->val = pro(this->val,n);}
    void operator/=(ll n){this->val = quo(this->val,n);}
    ll fac(ll x){
        //x! mod mod
        if(factable.size()<=x){
            ll s = factable.size();
            FOR(i,s,x+1){
                factable.push_back(pro(i,factable.back()));
                invtable.push_back(quo(1,factable.back()));
            }
        }
        if(x<0) return 1;
        else return factable[x];
    }
    ll facinv(ll x){
        if(invtable.size()<=x){
            ll s = invtable.size();
            FOR(i,s,x+1){
                factable.push_back(pro(i,factable.back()));
                invtable.push_back(quo(1,factable.back()));
            }
        }
        if(x<0) return 1;
        else return invtable[x];
    }
    ll com(ll x,ll y){
        //xCy mod mod = x!/((x-y)!*y!) mod mod
        if(x-y<y)return com(x,x-y);
        return pro(fac(x),pro(facinv(x-y),facinv(y)));
    }
};

string s;
ll q=0;
ll sa[100005];
ll sb[100005];
ll sc[100005];
ll fa[100005];
ll fb[100005];
ll fc[100005];

ll det(char a,char b,char c){
    //i<j<k and s[i]==a and s[j]==b and s[k]==cを満たすi,j,kの場合の数を返す
    ll rtn = 0;
    
    return rtn;
}

int main(){
    cin >> s;
    ll n = s.size();
    vector<ll> fa,fb,fc,fq;
    vector<ll> sa(n+1,0),sb(n+1,0),sc(n+1,0),sq(n+1,0);
    REP(i,n){
        if(s[i]=='A'){
            fa.push_back(i);
            sa[i+1] = sa[i]+1;
        }else{
            sa[i+1] = sa[i];
        }
        if(s[i]=='B'){
            fb.push_back(i);
            sb[i+1] = sb[i]+1;
        }else{
            sb[i+1] = sb[i];
        }
        if(s[i]=='C'){
            fc.push_back(i);
            sc[i+1] = sc[i]+1;
        }else{
            sc[i+1] = sc[i];
        }
        if(s[i]=='?'){
            fq.push_back(i);
            sq[i+1] = sq[i]+1;
            q++;
        }else{
            sq[i+1] = sq[i];
        }
    }
    vector<ll> sbc(n+1,0),sqc(n+1,0),sbq(n+1,0),sqq(n+1,0);
    REP(i,n){
        if(s[i]=='B'){
            sbc[i+1] = sbc[i]+sc[n]-sc[i+1];
            sbq[i+1] = sbq[i]+sq[n]-sq[i+1];
        }else{
            sbc[i+1] = sbc[i];
            sbq[i+1] = sbq[i];
        }
        if(s[i]=='?'){
            sqc[i+1] = sqc[i]+sc[n]-sc[i+1];
            sqq[i+1] = sqq[i]+sq[n]-sq[i+1];
        }else{
            sqc[i+1] = sqc[i];
            sqq[i+1] = sqq[i];
        }
    }
    Modulo res;
    res = 0;
    
    Modulo abc;
    abc = 0;
    REP(i,fa.size()){
        abc += sbc[n]-sbc[fa[i]+1];
    }
    abc*=abc.pow(3,q);
    res+=abc.val;
    
    Modulo qbc;
    qbc = 0;
    REP(i,fq.size()){
        qbc += sbc[n]-sbc[fq[i]+1];
    }
    qbc*=qbc.pow(3,q-1);
    res+=qbc.val;
    
    Modulo aqc;
    aqc = 0;
    REP(i,fa.size()){
        aqc += sqc[n]-sqc[fa[i]+1];
    }
    aqc*=aqc.pow(3,q-1);
    res+=aqc.val;
    
    Modulo abq;
    abq = 0;
    REP(i,fa.size()){
        abq += sbq[n]-sbq[fa[i]+1];
    }
    abq*=abq.pow(3,q-1);
    res+=abq.val;
    
    Modulo aqq;
    aqq = 0;
    REP(i,fa.size()){
        aqq += sqq[n]-sqq[fa[i]+1];
    }
    aqq*=aqq.pow(3,q-2);
    res+=aqq.val;
    
    Modulo qbq;
    qbq = 0;
    REP(i,fq.size()){
        qbq += sbq[n]-sbq[fq[i]+1];
    }
    qbq*=qbq.pow(3,q-2);
    res+=qbq.val;
    
    Modulo qqc;
    qqc = 0;
    REP(i,fq.size()){
        qqc += sqc[n]-sqc[fq[i]+1];
    }
    qqc*=qqc.pow(3,q-2);
    res+=qqc.val;
    
    Modulo qqq;
    qqq = 0;
    REP(i,fq.size()){
        qqq += sqq[n]-sqq[fq[i]+1];
    }
    qqq*=qqq.pow(3,q-3);
    res+=qqq.val;
    
    put(res.val);
    
    return 0;
}


Measured memory_bytes: 7800