#include<iostream>
#include<string>
#include<vector>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; }

template<int MOD>
struct ModInt {
	static const int kMod = MOD;
	unsigned x;
	ModInt() :x(0) {}
	ModInt(signed x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; }
	ModInt(signed long long x_) { x_ %= MOD; if (x_ < 0)x_ += MOD; x = x_; }
	int get()const { return (int)x; }
	ModInt& operator+=(ModInt m) { if ((x += m.x) >= MOD)x -= MOD; return *this; }
	ModInt & operator-=(ModInt m) { if ((x += MOD - m.x) >= MOD)x -= MOD; return *this; }
	ModInt & operator*=(ModInt m) { x = (unsigned long long)x * m.x % MOD; return *this; }
	ModInt& operator/=(ModInt m) { return *this *= m.inverse(); }
	ModInt operator+(ModInt m)const { return ModInt(*this) += m; }
	ModInt operator-(ModInt m)const { return ModInt(*this) -= m; }
	ModInt operator*(ModInt m)const { return ModInt(*this) *= m; }
	ModInt operator/(ModInt m)const { return ModInt(*this) /= m; }
	ModInt operator-()const { return ModInt(MOD - (signed)x); }
	bool operator==(ModInt m)const { return x == m.x; }
	bool operator!=(ModInt m)const { return x != m.x; }
	ModInt inverse()const {
		signed a = x, b = MOD, u = 1, v = 0;
		while (b) {
			signed t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		if (u < 0)u += MOD;
		return ModInt(u);
	}
};
template<int MOD>
ostream& operator<<(ostream & os, const ModInt<MOD> & m) { return os << m.x; }
template<int MOD>
istream& operator>>(istream & is, ModInt<MOD> & m) { signed long long s; is >> s; m = ModInt<MOD>(s); return is; };

template<int MOD>
ModInt<MOD> pow(ModInt<MOD> a, unsigned long long k) {
	ModInt<MOD> r = 1;
	while (k) {
		if (k & 1)r *= a;
		a *= a;
		k >>= 1;
	}
	return r;
}

using mint = ModInt<MOD>;


int main() {
	string S; cin >> S;
	int n = S.size();
	vector<vector<vector<mint>>> dp(n + 1, vector<vector<mint>>(4, vector<mint>(4)));

	dp[0][0][0] = 1;
	rep(i, 0, n) {
		if (S[i] == 'A') {
			rep(k, 0, 4) {
				dp[i + 1][0][k] += dp[i][0][k];
				dp[i + 1][1][k] += dp[i][0][k] + dp[i][1][k];
				dp[i + 1][2][k] += dp[i][2][k];
				dp[i + 1][3][k] += dp[i][3][k];
			}
		}
		else if (S[i] == 'B') {
			rep(k, 0, 4) {
				dp[i + 1][0][k] += dp[i][0][k];
				dp[i + 1][1][k] += dp[i][1][k];
				dp[i + 1][2][k] += dp[i][1][k] + dp[i][2][k];
				dp[i + 1][3][k] += dp[i][3][k];
			}
		}
		else if (S[i] == 'C') {
			rep(k, 0, 4) {
				dp[i + 1][0][k] += dp[i][0][k];
				dp[i + 1][1][k] += dp[i][1][k];
				dp[i + 1][2][k] += dp[i][2][k];
				dp[i + 1][3][k] += dp[i][2][k] + dp[i][3][k];
			}
		}
		else {
			rep(k, 0, 4) {
				if (k <= 2) {
					dp[i + 1][1][k + 1] += dp[i][0][k];
					dp[i + 1][2][k + 1] += dp[i][1][k];
					dp[i + 1][3][k + 1] += dp[i][2][k];
				}
				dp[i + 1][0][k] += dp[i][0][k];
				dp[i + 1][1][k] += dp[i][1][k];
				dp[i + 1][2][k] += dp[i][2][k];
				dp[i + 1][3][k] += dp[i][3][k];
			}
		}
	}
	vector<mint> p(n + 1);
	p[0] = 1;
	rep(i, 0, n)
		p[i + 1] = p[i] * 3;
	mint ans = 0;

	int m = 0;
	rep(i, 0, n)if (S[i] == '?')m++;

	rep(k, 0, 4) {
		if (m - k < 0)break;
		//cerr << dp[n][3][k] << " " << m - k << " " << p[m - k] << endl;
		ans += dp[n][3][k] * p[m - k];
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 26496