#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define ios        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll int hell=1000000007;
ll power(ll a,ll x,ll hell)
{
    if(x==0)
    return 1ll;
    
    if(x%2==0)
    {
        ll t=power(a,x/2,hell);
        t=((t*t)%hell);
        return t;
    }
    else
    {
        ll t=power(a,x/2,hell);
        t=((t*t)%hell);
        t=((t*a)%hell);
        return t;
    }
}
int main()
{
    ios
    string s;
    cin>>s;

    int n=s.size();
    ll int a[n],q[n],q1[n],c[n];
    ll int cnt=0,cnt1=0;
    ll int b[n]={0},b1[n]={0};

    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        cnt++;
        if(s[i]=='?')
        cnt1++;

        a[i]=cnt;
        q[i]=cnt1;
        if(i>=1)
        {
            if(s[i]=='B')
            b[i]=b[i-1]+a[i];
            else
            b[i]=b[i-1];
        }
    }

    ll int res=cnt1;
    cnt=0,cnt1=0;

    for(int i=n-1;i>=0;i--)
    {
        if(s[i]=='C')
        cnt++;
        if(s[i]=='?')
        cnt1++;

        q1[i]=cnt1;
        c[i]=cnt;
        if(i<n-1)
        {
            if(s[i]=='B')
            b1[i]=b1[i+1]+c[i];
            else
            b1[i]=b1[i+1];
        }
    }
    
    ll int ans=0,t,x,y,p,r;
    //cout<<q[2]<<endl;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='B')
        {
            x=a[i];
            y=q[i];
            p=c[i];
            r=q1[i];

            t=power(3ll,res,hell);
            x=((x*p)%hell);
            t=((t*x)%hell);
            ans+=t;
            ans=ans%hell;

            if(y>=1&&r>=1)
            {
                t=power(3ll,res-2,hell);
                x=((y*r)%hell);
                t=((t*x)%hell);
                ans+=t;
                ans=ans%hell;
            }
        }
        else if(s[i]=='?')
        {
            x=a[i];
            y=q[i];
            p=c[i];
            r=q1[i];

            t=power(3ll,res-1,hell);
            x=((x*p)%hell);
            t=((t*x)%hell);
            ans+=t;
            ans=ans%hell;

            t=power(3ll,res-1,hell);
            t=(((b[i]+b1[i])*t)%hell);
            ans+=t;
            ans=ans%hell;
        }
        else if(s[i]=='A')
        {
            x=q1[i];
            if(x>=2)
            {
                t=power(3ll,res-2,hell);
                x=(((x*(x-1))/2)%hell);
                t=((t*x)%hell);
                ans+=t;
                ans=ans%hell;
            }
        }
        else if(s[i]=='C')
        {
            x=q[i];
            if(x>=2)
            {
                t=power(3ll,res-2,hell);
                x=(((x*(x-1))/2)%hell);
                t=((t*x)%hell);
                ans+=t;
                ans=ans%hell;
            }
        }
    }
    if(res>=3)
    {
        t=power(3ll,res-3,hell);
        x=((((res*(res-1))*(res-2))/6)%hell);
        t=((t*x)%hell);
        ans+=t;
        ans=ans%hell;
    }
    cout<<ans;
}

Measured memory_bytes: 5200