#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#include"debug_deque.hpp"
#define vector DebugDeque
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ in >> p.first >> p.second; return in; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }

ll modPow(ll base, ll ex, ll m) {
	ll ret = 1;
	while(ex){
		if(ex & 1){
			ret = ret * base % m;
		}
		ex >>= 1;
		base = base * base % m;
	}
	return ret;
}

ll modFact(ll n, ll m){
	if(!n){ return 1; }
    static vector<ll> memo = {1, 1};
    if(n < (ll)memo.size()){ return memo[n]; }
    for(int i=memo.size();i<=n;i++){
        memo.push_back(memo.back() * i % m);
    }
    return memo[n];
}

pair<ll, pair<ll, ll>> exGCD(ll a, ll b){
	bool inv = false;
	if(a < b){
		swap(a, b);
		inv = true;
	}
	vector<ll> r = {a, b}, ks;
	while(b){
		ks.push_back(a / b);
		r.push_back(a = a % b);
		swap(a, b);
	}
	ll gcd = a;

	ll x = 0, y = 1, u = 1, v = -ks[0];
	for(unsigned long i=1;i<ks.size();i++){
		ll k = ks[i];
		ll x2 = u, y2 = v,
		   u2 = x - k * u, v2 = y - k * v;
		x = x2; y = y2; u = u2; v = v2;
	}

	if(inv){
		swap(x, y);
	}
	return {gcd, {x, y}};
}

ll modInv(ll n, ll m){
	return modPow(n, m-2, m);
	auto p = exGCD(n, m);
	return (p.second.first + m) % m;
}

ll modC(ll n, ll k, ll m){
	return modFact(n, m) * modInv(modFact(k, m) * modFact(n-k, m) % m, m) % m;
}

// template <ll M>
struct ModInt {
    ll num;
    ModInt(): num(0){ }
    ModInt(ll num): num(num){ }
    ModInt operator + (ModInt n){ return (num + n.num) % M; }
    ModInt operator - (ModInt n){ return (num - n.num) % M; }
    ModInt operator * (ModInt n){ return (num * n.num) % M; }
    ModInt operator / (ModInt n){ return num * modInv(n.num, M) % M; }
    bool operator < (ModInt n){ return num < n.num; }
    bool operator > (ModInt n){ return num > n.num; }
    bool operator <= (ModInt n){ return num <= n.num; }
    bool operator >= (ModInt n){ return num >= n.num; }
    bool operator == (ModInt n){ return num == n.num; }
    bool operator != (ModInt n){ return num != n.num; }
    ModInt operator += (ModInt n){ return num = (num + n.num) % M; }
    ModInt operator -= (ModInt n){ return num = (num - n.num) % M; }
    ModInt operator *= (ModInt n){ return num = (num * n.num) % M; }
    ModInt operator /= (ModInt n){ return num = num * modInv(n.num, M) % M; }
    friend ModInt operator + (ll a, ModInt b){ return (a + b.num) % M; }
    friend ModInt operator - (ll a, ModInt b){ return (a - b.num) % M; }
    friend ModInt operator * (ll a, ModInt b){ return (a * b.num) % M; }
    friend ModInt operator / (ll a, ModInt b){ return a * modInv(b.num, M) % M; }
    friend bool operator < (ll a, ModInt b){ return a < b.num; }
    friend bool operator > (ll a, ModInt b){ return a > b.num; }
    friend bool operator <= (ll a, ModInt b){ return a <= b.num; }
    friend bool operator >= (ll a, ModInt b){ return a >= b.num; }
    friend bool operator == (ll a, ModInt b){ return a == b.num; }
    friend bool operator != (ll a, ModInt b){ return a != b.num; }
    friend ostream& operator << (ostream& out, ModInt& e) { return out << e.num; }
    friend ModInt modPow(ModInt base, ModInt ex){ return modPow(base.num, ex.num, M); }
    friend ModInt modFact(ModInt n){ return modFact(n.num, M); }
    friend pair<ModInt, pair<ModInt, ModInt>> exGCD(ModInt a, ModInt b){ return exGCD(a.num, b.num); }
    friend ModInt modInv(ModInt n){ return modInv(n.num, M); }
    friend ModInt modC(ModInt n, ModInt k){ return modC(n.num, k.num, M); }
};
using mint = ModInt;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s; cin >> s;
    int n = s.size();
    vector<mint> als(n), crs(n), qls(n), qrs(n);
    for(int i = 0; i < n; i++){
        als[i] = (s[i] == 'A') + (i ? als[i - 1] : 0);
        qls[i] = (s[i] == '?') + (i ? qls[i - 1] : 0);
    }
    for(int i = n - 1; i >= 0; i--){
        crs[i] = (s[i] == 'C') + (i + 1 < n ? crs[i + 1] : 0);
        qrs[i] = (s[i] == '?') + (i + 1 < n ? qrs[i + 1] : 0);
    }
    printde(als, crs, qls, qrs);
    mint ans = 0;
    for(int i = 1; i < n - 1; i++){
        if(s[i] == 'B' || s[i] == '?'){
            mint l = als[i] * modPow(3, qls[i - 1]) + qls[i - 1] * (qls[i - 1] > 0 ? modPow(3, qls[i - 1] - 1) : 0),
                 r = crs[i] * modPow(3, qrs[i + 1]) + qrs[i + 1] * (qrs[i + 1] > 0 ? modPow(3, qrs[i + 1] - 1) : 0);
            ans += l * r ;
        }
    }
    cout << ans << '\n';
}


Measured memory_bytes: 3664