#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
const ll INF = 1e9;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
template<uint_fast64_t Modulas = 1000000007ul>
class ModInt {
    using u64 = uint_fast64_t;
public:
    u64 x;
 
    constexpr ModInt() : x(0) {}
    constexpr ModInt(const u64 x) : x(x % Modulas) {}
    constexpr ModInt operator+(const ModInt rhs) const noexcept {
        return ModInt(*this) += rhs;
    }
    constexpr ModInt operator-(const ModInt rhs) const noexcept {
        return ModInt(*this) -= rhs;
    }
    constexpr ModInt operator*(const ModInt rhs) const noexcept {
        return ModInt(*this) *= rhs;
    }
    constexpr ModInt operator/(const ModInt rhs) const noexcept {
        return ModInt(*this) /= rhs;
    }
    constexpr ModInt operator/(const long long rhs) const noexcept {
        return ModInt(*this) /= rhs;
    }
    constexpr ModInt operator+=(const ModInt rhs) noexcept {
        x += rhs.x;
        if (x >= Modulas) x -= Modulas;
        return *this;
    }
    constexpr ModInt operator+=(const long long rhs) noexcept {
        auto hs = ModInt<Modulas>(rhs);
        (*this) += hs;
        return *this;
    }
    constexpr ModInt operator-=(const ModInt rhs) noexcept {
        if (x < rhs.x) x += Modulas;
        x -= rhs.x;
        return *this;
    }
    constexpr ModInt operator-=(const long long rhs) noexcept {
        auto hs = ModInt<Modulas>(rhs);
        (*this) -= hs;
        return *this;
    }
    constexpr ModInt operator*=(const ModInt rhs) noexcept {
        x = x * rhs.x % Modulas;
        return *this;
    }
    constexpr ModInt operator*=(const long long rhs) noexcept {
        auto hs = ModInt<Modulas>(rhs);
        (*this) *= hs;
        return *this;
    }
    constexpr ModInt &operator/=(ModInt rhs) noexcept {
        u64 exp = Modulas - 2;
        while (exp > 0) {
            if (exp & 1ul) *this *= rhs;
            rhs *= rhs;
            exp >>= 1ul;
        }
        return *this;
    }
    constexpr ModInt &operator/=(long long rhs) noexcept {
        auto hs = ModInt<Modulas>(rhs);
        (*this) /= hs;
        return *this;
    }
 
    constexpr ModInt &operator++() noexcept {
        x++;
        if (x >= Modulas) x -= Modulas;
        return *this;
    }
    constexpr ModInt &operator--() noexcept {
        if (x == 0) x += Modulas;
        x--;
        return *this;
    }
    constexpr bool operator<(const ModInt rhs) const noexcept {
        return x < rhs.x;
    }
    constexpr bool operator==(const ModInt rhs) const noexcept {
        return this->x == rhs.x;
    }
    constexpr bool operator!=(const ModInt rhs) const noexcept {
        return !(*this == rhs);
    }
    friend istream &operator>>(istream &in, ModInt &m) {
        in >> m.x;
        m.x %= Modulas;
        return in;
    }
    friend ostream &operator<<(ostream &out, const ModInt &p) {
        out << p.x;
        return out;
    }
    constexpr ModInt pow(u64 p) const {
        ModInt ret(1);
        ModInt mul(x);
        while (p > 0) {
            if (p & 1ul) ret *= mul;
            mul *= mul;
            p >>= 1ul;
        }
        return ret;
    }
    constexpr ModInt operator~() const noexcept {
        u64 exp = Modulas - 2;
        return pow(exp);
    }
};
int main(){
    string s;
    cin >> s;
    int n = s.size(),f;
    ModInt<MOD> dp[n+1][4];
    dp[0][0] = 1;
    rep(i,n){
        if (s[i] != '?'){
            f = s[i]-'A'+1;
            rep(j,4){
                if (j == f) dp[i+1][j] = dp[i][j-1]+dp[i][j];
                else dp[i+1][j] = dp[i][j];
            }
        }
        else{
            dp[i+1][0] = dp[i][0]*3;
            rep(j,1,4) dp[i+1][j] = dp[i][j-1]+dp[i][j]*3;
        }
    }
    PRINT(dp[n][3]);
}


Measured memory_bytes: 3584