#include <algorithm>
#include <cassert>
// #include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
// #include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
*/
using namespace std;

const double PI = 2 * acos(0);

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppiipii;
typedef pair<int, ppiipii> pippiipii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;
typedef pair<int, ppiii> pippiii;
typedef pair<int, ll> pill;
typedef pair<int, double> pid;
typedef pair<double, double> pdd;
typedef pair<double, pdd> pddd;
typedef pair<string, string> pss;
// note to self, the below two triggers someone
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef long double ld;

char buf[100005];
const int MOD = 1000000007;

/*

ll resolve(int a, int b, int c, int d) {

}

ll slow(int a, int b, int c, int d) {
    ll amt = 0;
    for(int i = 0; i <= b; i++) {
        for(int j = 0; j <= d; j++) {
            ll inc = (a+i) * (ll)(c+j);
            int other = b-i+d-j;
            while(other--) {
                inc *= 2;
                inc %= MOD;
            }
            amt += inc;
            amt %= MOD;
        }
    }
    return amt;
}

*/

int main() {
    scanf("%s", buf);
    int len = strlen(buf);
    ll ret = 0;
    ll a = 0;
    ll ab = 0;
    ll did = 1;
    for(int i = 0; i < len; i++) {
        if(buf[i] == 'A') {
            a += did;
            a %= MOD;
        }
        else if(buf[i] == 'B') {
            ab += a;
            ab %= MOD;
        }
        else if(buf[i] == 'C') {
            ret += ab;
            ret %= MOD;
        }
        else {
            ll na = 3*a+did;
            ll nab = 3*ab+a;
            ll nret = 3*ret+ab;
            a=na;
            ab=nab;
            ret=nret;
            did *= 3;
            did %= MOD;
            ret %= MOD;
            ab %= MOD;
            a %= MOD;
        }
        // printf("%lld %lld %lld\n", a, ab, ret);
    }
    /*
????C?????B??????A???????
1 0 0
4 1 0
13 7 1
40 34 10
40 34 44
*/
    // 2291979612924
    // 430275431332
    ret %= MOD;
    printf("%lld\n", ret);
    /*
    int a = 0;
    int b = 0;
    int c = 0;
    int q = 0;
    for(int i = 0; i < len; i++) {
        if(buf[i] == 'A') a++;
        if(buf[i] == 'B') b++;
        if(buf[i] == 'C') c++;
        if(buf[i] == '?') q++;
    }
    int ap = 0;
    int bp = 0;
    int cp = 0;
    int qp = 0;
    for(int i = 0; i < len; i++) {
        if(buf[i] == 'A') a--;
        if(buf[i] == 'B') b--;
        if(buf[i] == 'C') c--;
        if(buf[i] == '?') q--;
        if(buf[i] == 'B' || buf[i] == '?') {
            ret += slow(ap, qp, c, q);
            ret %= MOD;
        }
        if(buf[i] == 'A') ap++;
        if(buf[i] == 'B') bp++;
        if(buf[i] == 'C') cp++;
        if(buf[i] == '?') qp++;
    }
    printf("%lld\n", ret);
    */
}


Measured memory_bytes: 384