#include "bits/stdc++.h"

using namespace std;

const long long MOD = 1e9 + 7;
const int MAX = 1e5 + 10;

/**
* Binary_Indexed_Tree(BIT)
* Fenwick Tree
* 1-indexed!!!
*/
template<typename T = int>
class BIT {
private:
    std::vector<T> bit;
    int N;
public:
    BIT() : N(-1) {}
    BIT(int n) : N(n), bit(n + 1, 0) {}
    //bit[point]にnum追加
    void add(int point, T num) {
        assert(N != -1 && 0 <= point && point <= N);
        while (point <= N) {
            (bit[point] += num) %= MOD;
            //(bit[point] += num);
            point += point & -point;
        }
    }
    //pointまでの値の和を返す
    T sum(int point) {
        assert(N != -1 && 0 <= point && point <= N);
        T res = 0;
        while (point > 0) {
            (res += bit[point]) %= MOD;
            //(res += bit[point]);
            point -= point & -point;
        }
        return res;
    }
    //left から right の区間和
    T sum(int left, int right) {
        return (this->sum(right) - this->sum(left - 1) + MOD) % MOD;
    }
};

string S;
int hatena;
long long pow3[MAX];
//long long dp[MAX][3];// dp[LENGTH][0: '', 1: 'A', 2: 'AB']

void make_pow3() {
    pow3[0] = 1;
    for (int i = 1; i < MAX; i++) {
        pow3[i] = pow3[i - 1] * 3 % MOD;
    }
}

long long get_pow3(int idx) {
    if (idx < 0) {
        return 0;
    } else {
        return pow3[idx];
    }
}

long long calc(string target) {
    const int h = count_if(begin(target), end(target), [](char c) { return c == '?'; });
    if (h > hatena) {
        return 0;
    }
    BIT<long long> bit[3];
    for (int i = 0; i < 3; i++) {
        bit[i] = BIT<long long>(MAX + 10);
    }
    for (int i = 0; i < S.size(); i++) {
        vector<long long> dp(3);
        for (int j = 0; j < 3; j++) {
            if (S[i] == target[j]) {
                dp[(j + 1) % 3] = bit[j].sum(MAX) + (j == 0);
            }
        }
        for (int j = 0; j < 3; j++) {
            bit[j].add(i + 1, dp[j]);
        }
        //for (int j = 0; j < 3; j++) cerr << dp[j] << " ";cout << endl;
    }
    return bit[0].sum(MAX) * pow3[hatena - h] % MOD;
}

void foo() {
    cin >> S;
    hatena = count_if(begin(S), end(S), [](char c) { return c == '?'; });
    string target = "ABC";
    long long ans = 0;
    for (int i = 0; i < (1 << 3); i++) {
        string t = target;
        for (int j = 0; j < 3; j++) {
            if (i & (1 << j)) {
                t[j] = '?';
            }
        }
        (ans += calc(t)) %= MOD;
        cerr << ans << endl;
        assert(i == 0 || target != t);
    }
    cout << ans << endl;
}

void bar () {
    cin >> S;
    long long ans = 0;
    const long long all_hatena = count_if(begin(S), end(S), [](char c) { return c == '?'; });
    const long long all_a = count_if(begin(S), end(S), [](char c) { return c == 'A'; });
    const long long all_b = count_if(begin(S), end(S), [](char c) { return c == 'B'; });
    const long long all_c = count_if(begin(S), end(S), [](char c) { return c == 'C'; });
    assert(all_hatena + all_a + all_b + all_c == S.length());
    long long a_cnt = 0, c_cnt = 0, h_cnt = 0;
    for (int i = 0; i < S.length(); i++) {
        if (0 < i && i < S.length() - 1 && (S[i] == 'B' || S[i] == '?')) {
            if (S[i] == '?') {
                long long pre = 0;
                // A?C
                (pre += a_cnt * (all_c - c_cnt) % MOD * get_pow3(all_hatena - 1) % MOD) %= MOD;
                // ??C
                (pre += h_cnt * (all_c - c_cnt) % MOD * get_pow3(all_hatena - 2) % MOD) %= MOD;
                // A??
                (pre += a_cnt * (all_hatena - h_cnt - 1) % MOD * get_pow3(all_hatena - 2) % MOD) %= MOD;
                // ???
                (pre += h_cnt * (all_hatena - h_cnt - 1) % MOD * get_pow3(all_hatena - 3) % MOD) %= MOD;

                (ans += pre) %= MOD;
            } else if (S[i] == 'B') {
                long long pre = 0;
                // ABC
                (pre += a_cnt * (all_c - c_cnt) % MOD * get_pow3(all_hatena) % MOD) %= MOD;
                // ?BC
                (pre += h_cnt * (all_c - c_cnt) % MOD * get_pow3(all_hatena - 1) % MOD) %= MOD;
                // AB?
                (pre += a_cnt * (all_hatena - h_cnt) % MOD * get_pow3(all_hatena - 1) % MOD) %= MOD;
                // ?B?
                (pre += h_cnt * (all_hatena - h_cnt) % MOD * get_pow3(all_hatena - 2) % MOD) %= MOD;

                (ans += pre) %= MOD;
            } else {
                assert(false);
            }
        }
        if (S[i] == 'A') {
            a_cnt++;
        } else if (S[i] == 'B') {

        } else if (S[i] == 'C') {
            c_cnt++;
        } else if (S[i] == '?') {
            h_cnt++;
        }
    }
    cout << ans << endl;
}

int main() {
    make_pow3();
    //foo();
    bar();
}


Measured memory_bytes: 1280