#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

struct mint {
    long long x;
    mint(long long x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
    	return *this;
    }
    mint& operator*=(const mint a) {
    	(x *= a.x) %= MOD;
    	return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
    	if (t&1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD-2);
    }
    mint& operator/=(const mint a) {
    	return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main(void){
    string S; cin >> S;
    int N = S.size();
    vector<mint> n_lq(N), n_rq(N);

    n_lq[0] = 0;
    REP(i,N-1)
        if(S[i] == '?') n_lq[i+1] = n_lq[i]+1;
        else n_lq[i+1] = n_lq[i];

    n_rq[N-1] = 0;
    REP(i,N-1)
        if(S[N-1-i] == '?') n_rq[N-2-i] = n_rq[N-1-i]+1;
        else n_rq[N-2-i] = n_rq[N-1-i];
    
    vector<mint> nA(N), nC(N);

    nA[0] = 0;
    REP(i,N-1){
        if(S[i] == '?') nA[i+1] = nA[i]*3 + mint(3).pow(n_lq[i].x);
        else if(S[i] == 'A') nA[i+1] = nA[i] + mint(3).pow(n_lq[i].x);
        else nA[i+1] = nA[i];
    }

    nC[N-1] = 0;
    for(int i=N-1; i>0; i--){
        if(S[i] == '?') nC[i-1] = nC[i]*3 + mint(3).pow(n_rq[i].x);
        else if(S[i] == 'C') nC[i-1] = nC[i] + mint(3).pow(n_rq[i].x);
        else nC[i-1] = nC[i];
    }

    mint ans(0);
    REP(i,N){
        if(S[i] == 'B' || S[i] == '?') ans += nA[i]*nC[i];
    }

    cout << ans.x << endl;

    return 0;
}

Measured memory_bytes: 3584