#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;

//struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
constexpr double PI  = acos(-1.0);
//constexpr int INF = INT_MAX;

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

constexpr int MOD = 1000000000 + 7;

// 1 <= k <= n <= 10^7
#define CNKMAX 100001
class CNK1 {
    std::vector<long long> fac, finv, inv;
    int MOD;
public:
    CNK1(int mod = 1000000007): MOD(mod), fac(CNKMAX), finv(CNKMAX), inv(CNKMAX) {
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for (int i = 2; i < CNKMAX; i++){
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }
    int get(const int &n, const int &k) const {
        if (n < k) return 0;
        if (n <= 0 || k < 0) return 0;
        return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
    }
};

long long power(long long x, long long n, long long mod = MOD) {
    if (n == 0) return 1;
    if (n % 2 == 0) return power(x * x % mod, n/2, mod);
    else return x * power(x, n-1, mod) % mod;
}

void solve() {
    string s;
    cin >> s;
    int n = s.size();
    VVI ru(4, VI(n));
    REP(i,0,n) {
        if (s[i] == '?') ru[3][i]++;
        else ru[s[i] - 'A'][i]++;
    }
    REP(i,1,n) {
        REP(j,0,4) ru[j][i] += ru[j][i-1];
    }
    LL ans = 0;
    CNK1 cnk;
    REP(i,1,n-1) {
        if (s[i] == 'B' || s[i] == '?') {
            int ac = ru[0][i-1], cc = ru[2][n-1] - ru[2][i];
            int qb = ru[3][i-1], qa = ru[3][n-1] - ru[3][i];
            //cerr << ac << " " << cc << " " << qb << " " << qa << endl;
            //ans = (ans + ru[0][i] * (ru[2][n-1] - ru[2][i])) % MOD;
            LL a = (power(3,qb) * (LL)ac) % MOD;
            // a = (a + power(3,qb) * (LL)qb / (LL)3) % MOD;
            if (qb > 0) a = (a + power(3,qb-1) * (LL)qb) % MOD;
            LL c = (power(3,qa) * (LL)cc) % MOD;
            // c = (c + power(3,qa) * (LL)qa / (LL)3) % MOD;
            if (qa > 0) c = (c + power(3,qa-1) * (LL)qa) % MOD;
            /*
            REP(i,0,qb+1) {
                a = (a + cnk.get(qb,i) * power(2,qb-i) * i) % MOD;
            }
            REP(i,0,qa+1) {
                c = (c + cnk.get(qa,i) * power(2,qa-i) * i) % MOD;
            }
            */
            //cerr << a << " " << c << endl;
            //dump(cnk.get(1,0));
            //dump(cnk.get(1,1));
            ans = (ans + (a * c)) % MOD;
        }
    }
    cout << ans << endl;
}

int main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 4344