#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define l_ength size
#define MOD (ll)(1e9+7)

int main(void){
int n,i,j;
ll dp[3][123456],t=1ll;;
fill(dp[0],dp[3],0ll);
string s;
cin>>s;
n=s.l_ength();
for(i=0;i<n;++i){
for(j=0;j<3;++j){
dp[j][i+1] = dp[j][i];
if(s[i]=='?'){
dp[j][i+1]*=3;
dp[j][i+1]%=MOD;
}
if(s[i]=='A'+j||s[i]=='?'){
dp[j][i+1]+=((j)?dp[j-1][i]:t);
dp[j][i+1]%=MOD;
}
}
if(s[i]=='?'){
t*=3;
t%=MOD;
}
}
cout<<dp[2][n]<<endl;
return 0;
}
