#include <iostream>
#include <fstream>
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <functional>
#include <string> 
#include <vector>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define REP(i,n) for(int i = 0; i < int(n); i++)
#define REREP(i,n) for(int i = int(n - 1); i >= 0; i--)
#define FOR(i, m, n) for(int i = int(m);i < int(n);i++)
#define REFOR(i, m, n) for(int i = int(n - 1);i >= int(m);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define fi first 
#define se second

#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VD vector<double>
#define VVD vector<vector<double>>
#define VVVD vector<vector<vector<double>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define VVVB vector<vector<vector<bool>>>
#define VS vector<string>
#define VVS vector<vector<string>>
#define VVVS vector<vector<vector<string>>>

#define PII pair<int,int>
#define PDD pair<double,double>
#define PLL pair<ll,ll>
#define VPII vector<pair<int,int>>
#define VVPII vector<vector<pair<int,int>>>
#define VVVPII vector<vector<vector<pair<int,int>>>>
#define VPLL vector<pair<ll,ll>>
#define VVPLL vector<vector<pair<ll,ll>>>
#define VVVPLL vector<vector<vector<pair<ll,ll>>>>

const ll HMOD = (ll)1e9 + 7;
const ll LMOD = 998244353;
const ll HINF = (ll)1e18;
const ll LINF = (ll)1e9;
const long double PI = 3.1415926535897932384626433;
const long double EPS = 1e-10;


void YN(bool flag) {
	cout << ((flag) ? "YES" : "NO") << endl;
}

void Yn(bool flag) {
	cout << ((flag) ? "Yes" : "No") << endl;
}

void yn(bool flag) {
	cout << ((flag) ? "yes" : "no") << endl;
}

ll rsm(ll x, ll n, ll quotient) {
	ll y = 1;
	while (n > 0) {
		if (n & 1) {
			y *= x;
			y %= quotient;
		}
		x *= x;
		x %= quotient;
		n >>= 1;
	}
	return y;
}

int main() {
	string S; cin >> S;
	int N = S.size();
	VL A(N,0), C(N,0), L(N,0), R(N,0);
	FOR(i, 0, N - 1) if (S[i] == 'A') A[i + 1]++;
	FOR(i, 0, N - 1) if (S[i] == '?') L[i + 1]++;
	for (int i = N - 1; i >= 1; --i) if (S[i] == 'C') C[i - 1]++;
	for (int i = N - 1; i >= 1; --i) if (S[i] == '?') R[i - 1]++;

	
	FOR(i, 1, N) A[i] += A[i - 1];
	for (int i = N - 2; i >= 0; --i) C[i] += C[i + 1];
	FOR(i, 1, N) L[i] += L[i - 1];
	for (int i = N - 2; i >= 0; --i) R[i] += R[i + 1];


	ll ans = 0;
	FOR(i, 1, N - 1) {
		if (S[i] != 'B' && S[i] != '?') continue;
		ll cnt = 0;

		cnt += ((((A[i] % HMOD) * (C[i] % HMOD)) % HMOD) * ((rsm(3, L[i], HMOD) * rsm(3, R[i], HMOD)) % HMOD))%HMOD;
		cnt %= HMOD;
		cnt += ((((A[i] % HMOD) * (R[i] % HMOD)) % HMOD) * ((rsm(3, L[i], HMOD) * rsm(3, max(R[i]-1,0LL), HMOD)) % HMOD)) % HMOD;
		cnt %= HMOD;
		cnt += ((((L[i] % HMOD) * (C[i] % HMOD)) % HMOD) * ((rsm(3, max(L[i]-1,0LL), HMOD) * rsm(3, R[i], HMOD)) % HMOD)) % HMOD;
		cnt %= HMOD;
		cnt += ((((L[i] % HMOD) * (R[i] % HMOD)) % HMOD) * ((rsm(3, max(L[i] - 1, 0LL), HMOD) * rsm(3, max(R[i] - 1, 0LL), HMOD)) % HMOD)) % HMOD;
		cnt %= HMOD;
		(ans += cnt) %= HMOD;
	}

	cout << ans << endl;



	
		
	return 0;
}



Measured memory_bytes: 3584