#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){
  string s;
  cin>>s;
  const Int MOD = 1e9+7;
  Int n=s.size();
  Int x=0,y=0,z=0,r=1;
  Int inv=(1+MOD)/3;
  for(Int i=0;i<n;i++){
    if(s[i]=='C'||s[i]=='?') z+=y*(s[i]=='?'?inv:1);
    if(s[i]=='B'||s[i]=='?') y+=x*(s[i]=='?'?inv:1);
    if(s[i]=='A'||s[i]=='?') x+=1*(s[i]=='?'?inv:1);
    if(s[i]=='?') r*=3;
    x%=MOD;y%=MOD;z%=MOD;r%=MOD;
  }
  z*=r;
  z%=MOD;
  cout<<z<<endl;
  return 0;
}