#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0 ; i < (int)(n) ; i++)
typedef long long ll;
typedef long double Double;

#ifndef INPUTS_DIR
#define INPUTS_DIR "./"
#endif

long long gcd(long long a, long long b) {
    return b ? gcd(b, a % b) : a;
}

long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b;
}

long long mul(long long a, long long b, const long long mod) {
    return b ? (mul(a * 2, b / 2, mod) + (b & 1 ? a : 0)) % mod : 0;
}

long long bpow(long long a, long long b, const long long mod) {
    return (b ? bpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) : 1) % mod;
}

long long inv(long long a, const long long mod) {
    return bpow(a, mod - 2, mod);
}

// to overflow
/*long long bpow(long long a,long long b,const long long mod){
	return (b?mul(bpow(mul(a,a,mod),b/2,mod),(b&1?a:1),mod):1)%mod;
}*/
class mInt {
public:
    static const long long mod = 1000000007;
    long long v;

    mInt() : v(0) {}

    mInt(long long v) : v((v % mod + mod) % mod) {}
};

mInt &operator+=(mInt &a, mInt b) { return a = a.v + b.v; }

mInt &operator-=(mInt &a, mInt b) { return a = a.v - b.v; }

mInt &operator*=(mInt &a, mInt b) { return a = a.v * b.v; }

mInt &operator/=(mInt &a, mInt b) { return a = a.v * inv(b.v, mInt::mod); }

mInt operator+(mInt a, mInt b) { return a += b; }

mInt operator-(mInt a, mInt b) { return a -= b; }

mInt operator*(mInt a, mInt b) { return a *= b; }

mInt operator/(mInt a, mInt b) { return a /= b; }

ostream &operator<<(ostream &os, const mInt &x) { return os << x.v; }


mInt sum[100010][4];

#define A 0
#define B 1
#define C 2
#define Q 3

mInt counter(int id, int l, int r) {
    return sum[r][id] - sum[l][id];
}

mInt th[100010];


void solve(string S) {
    th[0] = 1;
    for (int i = 1; i < 100010; i++) {
        th[i] = th[i - 1] * 3;
    }
    for (int i = 0; i < S.size(); i++) {
        if (S[i] == 'A') S[i] = 0;
        if (S[i] == 'B') S[i] = 1;
        if (S[i] == 'C') S[i] = 2;
        if (S[i] == '?') S[i] = 3;
    }
    for (int j = 0; j < 4; j++) {
        for (int i = 0; i < S.size(); i++) {
            sum[i + 1][j] += sum[i][j] + (S[i] == j);
        }
    }
    mInt ans = 0;
    for (int i = 0; i < S.size(); i++) {
        if (S[i] == 1 || S[i] == 3) {
            mInt NA = counter(A, 0, i);
            mInt NAQ = counter(Q, 0, i);
            mInt NC = counter(C, i + 1, (int) S.size());
            mInt NCQ = counter(Q, i + 1, (int) S.size());
            mInt sub = 0;

            sub += NA * NC * th[NAQ.v] * th[NCQ.v]; // 0 0

            if (NAQ.v) {
                sub += NAQ.v * th[NAQ.v - 1] * NC * th[NCQ.v]; // 1 0
            }

            if (NCQ.v) {
                sub += NA * th[NAQ.v] * NCQ.v * th[NCQ.v - 1]; // 0 1
            }

            if (NAQ.v && NCQ.v) {
                sub += NAQ.v * th[NAQ.v - 1] * NCQ.v * th[NCQ.v - 1]; // 1 1
            }
            ans += sub;
        }

    }
    cout << ans << endl;


}


void main_(istream &cin) {
    string S;
    cin >> S;
    solve(S);
}

int main() {
#ifdef INTELLIJ
    ifstream ifs(string(INPUTS_DIR) + "in_1.txt");
    if (ifs.is_open()) {
        main_(ifs);
        ifs.close();
    } else {
        cerr << "Error: no input." << endl;
    }
#else
    ios::sync_with_stdio(false);
    cin.tie(0);
    main_(cin);
#endif
}





Measured memory_bytes: 4480