#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

const int mod = 1000000007;
// const int mod = 998244353;
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 { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= 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;
    }
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

// int main() {
//     ios::sync_with_stdio(false);
//     cin.tie(0);
    
//     string s;
//     cin >> s;
//     int n = sz(s);
//     vll la(n+1, 0), rc(n+1, 0);
//     rep(i, n) la[i+1] = la[i] + (s[i] == 'A');
//     for (int i = n-1; i >= 0; --i) rc[i] = rc[i+1] + (s[i] == 'C');
//     ll aq = 0;
//     rep(i, n) if (s[i] == '?') aq++;
//     ll lq = 0;
//     ll rq = aq;
//     mint ans = 0;
//     rep(i, n) {
//         if (s[i] == 'B') {
//             ans += mint(la[i]) * mint(rc[i]) * mint(3).pow(aq);
//             if (aq - 1 >= 0) ans += mint(lq) * mint(rc[i]) * mint(3).pow(aq-1);
//             if (aq - 1 >= 0) ans += mint(la[i]) * mint(rq) * mint(3).pow(aq-1);
//             if (aq - 2 >= 0) ans += mint(lq) * mint(rq) * mint(3).pow(aq-2);
//         } else if (s[i] == '?') {
//             rq--;
//             aq--;
//             ans += mint(la[i]) * mint(rc[i]) * mint(3).pow(aq);
//             if (aq - 1 >= 0) ans += mint(lq) * mint(rc[i]) * mint(3).pow(aq-1);
//             if (aq - 1 >= 0) ans += mint(la[i]) * mint(rq) * mint(3).pow(aq-1);
//             if (aq - 2 >= 0) ans += mint(lq) * mint(rq) * mint(3).pow(aq-2);
//             aq++;
//             lq++;
//         }
//     }
//     cout << ans << endl;
// }

int main() {
    string s;
    cin >> s;
    int n = sz(s);
    vector<vector<mint>> dp(n+1, vector<mint>(4, 0));
    dp[0][0] = 1;
    for (int i = 0; i < n; ++i) {
        if (s[i] == 'A' || s[i] == '?') {
            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];
            dp[i+1][1] += dp[i][0];
        }
        if (s[i] == 'B' || s[i] == '?') {
            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];
            dp[i+1][2] += dp[i][1];
        }
        if (s[i] == 'C' || s[i] == '?') {
            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];
            dp[i+1][3] += dp[i][2];
        }
    }
    cout << dp[n][3] << endl;
}

Measured memory_bytes: 7424