#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

const int mod = 1000000007;
class modint {
public:
    ll a = 0;
    modint(ll x = 0) {while(x < 0)x += mod;a = x%mod;}
    modint(const modint &r) {a = r.a;}
    modint operator -(){return modint(-a);}
    modint operator +(const modint &r) {return modint(*this)+=r;}
    modint operator -(const modint &r) {return modint(*this)-=r;}
    modint operator *(const modint &r) {return modint(*this)*=r;}
    modint &operator +=(const modint &r){
        a += r.a;
        if(a >= mod) a -= mod;
        return *this;
    }
    modint &operator -=(const modint &r) {
        a -= r.a;
        if(a < 0) a += mod;
        return *this;
    }
    modint &operator *=(const modint &r) {
        a = a * r.a % mod;
        return *this;
    }
    modint pow(ll r) const {
        if (!r) return 1;
        modint x = pow(r >> 1);
        x *= x;
        if (r & 1) x *= *this;
        return x;
    }
    modint inverse() const {
        return pow(mod-2);
    }
    modint& operator/=(const modint r) {
        return (*this) *= r.inverse();
    }
    modint operator/(const modint r) const {
        modint res(*this);
        return res /= r;
    }
};
istream &operator>>(istream &is, modint &x) {
    ll t;
    is >> t;
    x = t;
    return is;
}
ostream &operator<<(ostream &os, const modint& x) {
    return os << x.a;
}

using mint = modint;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> s;
    int n = size(s);
    VV<mint> dp(n+1,V<mint>(4, 0));
    dp[0][0] = 1;
    rep(i, n) {
        dp[i+1][0] += dp[i][0];
        dp[i+1][1] += dp[i][1];
        dp[i+1][2] += dp[i][2];
        dp[i+1][3] += dp[i][3];
        if(s[i] == 'A') dp[i+1][1] += dp[i][0];
        if(s[i] == 'B') dp[i+1][2] += dp[i][1];
        if(s[i] == 'C') dp[i+1][3] += dp[i][2];
        if(s[i] == '?') {
            dp[i+1][0] += dp[i][0] * 2;
            dp[i+1][1] += dp[i][0] + dp[i][1] * 2;
            dp[i+1][2] += dp[i][1] + dp[i][2] * 2;
            dp[i+1][3] += dp[i][2] + dp[i][3] * 2;
        }
    }
    cout << dp[n][3] << endl;
}

Measured memory_bytes: 10324