#include<iostream>
#include<string>
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef signed long long ll;

using namespace std;

int main(){
	ll a,ab,abc,all,M,x;
	M=1000000007;
	a=0;
	ab=0;
	abc=0;
	all=1;
	
	string S;
	cin >> S;
	ll l=S.length();
	
	for( int i=0; i<l ; ++i){
		x=S[i];
		if (x=='A') a+=all;
		if (x=='B') ab+=a;
		if (x=='C') abc+=ab;
		
		if (x=='?') {
			abc=abc*3+ab;
			ab=ab*3+a;
			a= a*3+all;
			all*=3;
		}
		abc%=M;
		ab%=M;
		a%=M;
		all%=M;	
	}
	cout << abc%M << endl;
}

