#include <bits/stdc++.h>
using namespace std;
using ll = long long;

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

template <ll mod>
class modint
{
public:
    constexpr modint(ll value = 0) : _value(value >= 0 ? value % mod : (value % mod) + mod) {}

    constexpr modint operator+() const { return *this; }
    constexpr modint operator-() const { return modint(-_value); }

    constexpr modint &operator+=(const modint &rhs) {
        if ((_value += rhs._value) >= mod) _value -= mod;
        return *this;
    }
    constexpr modint &operator-=(const modint &rhs) {
        if ((_value += mod - rhs._value) >= mod) _value -= mod;
        return *this;
    }
    constexpr modint &operator*=(const modint &rhs) {
        (_value *= rhs._value) %= mod;
        return *this;
    }
    constexpr modint &operator/=(const modint &rhs) {
        *this *= rhs.inv();
        return *this;
    }
    constexpr modint operator+(const modint &rhs) const { return modint(*this) += rhs; }
    constexpr modint operator-(const modint &rhs) const { return modint(*this) -= rhs; }
    constexpr modint operator*(const modint &rhs) const { return modint(*this) *= rhs; }
    constexpr modint operator/(const modint &rhs) const { return modint(*this) /= rhs; }

    constexpr modint operator++() { *this += 1; return *this; }
    constexpr modint operator--() { *this -= 1; return *this; }
    constexpr modint operator++(int) { modint tmp = *this; ++*this; return tmp; }
    constexpr modint operator--(int) { modint tmp = *this; --*this; return tmp; }

    constexpr bool operator==(const modint &rhs) const { return _value == rhs._value; }
    constexpr bool operator!=(const modint &rhs) const { return _value != rhs._value; }

    constexpr modint inv() const {
        ll a = _value, b = mod, u = 1LL, v = 0LL;
        while (b > 0) {
            ll tmp = a / b;
            a -= tmp * b; swap(a, b);
            u -= tmp * v; swap(u, v);
        }
        return modint(u);
    }

    constexpr modint pow(ll n) const {
        modint e = *this, res = 1;
        while (n) {
            if (n & 1) res *= e;
            e *= e, n >>= 1;
        }
        return res;
    }

    friend ostream &operator<<(ostream &os, const modint &m) {
        os << m._value;
        return os;
    }

    friend istream &operator>>(istream &is, modint &m) {
        ll in;
        is >> in;
        m = modint(in);
        return is;
    }

private:
    ll _value;
};
using mint = modint<1000000007>;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    string s; cin >> s;
    int n = s.length();

    vector<int> a(n + 1), c(n + 1), q(n + 1);
    for (int i = 0; i < n; i++) {
        a[i + 1] = a[i] + (s[i] == 'A');
        c[i + 1] = c[i] + (s[i] == 'C');
        q[i + 1] = q[i] + (s[i] == '?');
    }

    mint ans = 0;
    for (int i = 1; i < n - 1; i++) {
        if (s[i] == 'B') {
            ans += mint(a[i]) * (c[n] - c[i + 1]) * mint(3).pow(q[n]);
            if (q[n] >= 1) ans += mint(a[i]) * (q[n] - q[i + 1]) * mint(3).pow(q[n] - 1);
            if (q[n] >= 1) ans += mint(q[i]) * (c[n] - c[i + 1]) * mint(3).pow(q[n] - 1);
            if (q[n] >= 2) ans += mint(q[i]) * (q[n] - q[i + 1]) * mint(3).pow(q[n] - 2);
        }
        if (s[i] == '?') {
            ans += mint(a[i]) * (c[n] - c[i + 1]) * mint(3).pow(q[n] - 1);
            if (q[n] >= 2) ans += mint(a[i]) * (q[n] - q[i + 1]) * mint(3).pow(q[n] - 2);
            if (q[n] >= 2) ans += mint(q[i]) * (c[n] - c[i + 1]) * mint(3).pow(q[n] - 2);
            if (q[n] >= 3) ans += mint(q[i]) * (q[n] - q[i + 1]) * mint(3).pow(q[n] - 3);
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1616