#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);
static mt19937 _g(time(nullptr));
 
inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

template< int mod >
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

const ll mod=1e9+7;
using modint = ModInt< mod >;


string S;
void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        output += string("/output/") + string(p.stem()) + string(".txt");
        freopen(input.c_str(), "r", stdin);
        freopen(output.c_str(), "w", stdout);
    #endif
    cin >> S;
}

int solve(){
    int n=S.length();
    vector<modint> dpz(n+1,0);
    vector<modint> dpA(n+1,0);
    vector<modint> dpAB(n+1,0);
    vector<modint> dpABC(n+1,0);
    dpz[0] = modint(1);
    dpA[0] = modint(0);
    dpAB[0] = modint(0);
    dpABC[0] = modint(0);
    for (int i = 0; i < n; i++)
    {
        dpz[i+1] += dpz[i];
        if(S[i]=='?'){dpz[i+1]*=modint(3);}
        // if(S[i]=='A'){dpA[i+1]+=dpz[i];}
    }
    for (int i = 0; i < n; i++)
    {
        dpA[i+1] += dpA[i];
        if(S[i]=='?'){dpA[i+1]*=modint(3); dpA[i+1]+=dpz[i];}
        if(S[i]=='A'){dpA[i+1]+=dpz[i];}
    }
    for (int i = 0; i < n; i++)
    {
        dpAB[i+1] += dpAB[i];
        if(S[i]=='?') {dpAB[i+1]*=modint(3); dpAB[i+1]+=dpA[i];}
        if(S[i]=='B') {dpAB[i+1]+=dpA[i];}
    }
    for (int i = 0; i < n; i++)
    {
        dpABC[i+1] += dpABC[i];
        if(S[i]=='?') {dpABC[i+1]*=modint(3); dpABC[i+1]+=dpAB[i];}
        if(S[i]=='C') {dpABC[i+1]+=dpAB[i];}
    }
    cout << dpABC[n] << endl;

    // vector<vector<modint>> dp(n+1,vector<modint>(4,0));
    // dp[n][3]=1;
    // for (int i = n-1; i >= 0; i--)
    // {
    //     for (int j = 3; j >= 0; j--)
    //     {
    //         dp[i][j] = dp[i+1][j]*((S[i]=='?')?3:1);
    //         if(j<3 && (S[i]=='?'||S[i]=="ABC"[j]))
    //             dp[i][j] += dp[i+1][j+1];
    //     }
        
    // }
    // cout << dp[0][0] << endl;    
    return 0;
}

#define rep(i,n) for(int i=0;i<n;i++)

bool isok(string s){
    char x='0';
    for (int i = 0; i < s.length(); i++)
    {
        if(s[i]==x) x++;
    }
    return x=='3';
    
}

void test(){
    int q=4;
    string s;
    rep(i,3) rep(j,3) rep(k,3) rep(l,3) 
    {
    s=to_string(i)+to_string(j)+to_string(k)+to_string(l);
    if(!isok(s)) continue;
    cout << s << endl;
    } 
}

int main()
{
    input();
    solve();
    // test();
    return 0;
}


Measured memory_bytes: 2000