#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long long L;
typedef string STR;
typedef char CH;
typedef double DB;
typedef void VD;
typedef bool BL;
#define N '\n'
#define S ' '
#define Z '0'
#define MAX (L(1e6)+7)
#define MOD (L(1e9)+7)
#define INF (L(1e18)+7)

#define V vector<L>
#define VV vector<V>
#define LI list<L>
#define LL list<LI>
#define DQ deque<L>
#define DD deque<DQ>
#define PR pair<L,L>
#define MP map<L,L>
#define SE set<L>
#define PQ priority_queue<L>
#define PQG priority_queue<L,V,greater<L>>
#define FL forward_list<L>

#define TTA template<class T,class...A>
#define TT template<class T>
#define TA template<class...A>
#define TL template<class T=L>
#define Tt T t
#define Aa A...a
#define Tt2 T&&t
#define Aa2 A&&...a
#define M(a) move(a)...

#define CO const
#define AU auto
#define CN continue
#define RT return
#define elif else if

#define IV inline VD
#define IL inline L
#define IB inline BL
#define ILV inline V
#define def inline AU

#define A1(a,b) L a[b]
#define A2(a,b,c) L a[b][c]
#define V1(a,b) V a(b)
#define V2(a,b,c) VV a;a=VV(b,V(c,0))

#define W(a) while(a)
#define F(a,b,c) for(L a=(b);a<(c);++a)
#define R(a,b) for(L a=0;a<(b);++a)
#define RP(a,b) for(L a=(b);a>=0;--a)
#define FE(a,b) for(auto a:b)
#define FE2(a,b) FE(X,b)FE(a,X)
#define FA(a,b) for(auto&a:b)
#define FA2(a,b) FA(X,b)FA(a,X)

#define LF(a) a.front()
#define LB(a) a.back()
#define PF(a,b) a.push_front(b)
#define PB(a,b) a.push_back(b)
#define PPF(a) a.pop_front()
#define PPB(a) a.pop_back()
#define ES(a,b) a.erase(b)
#define ES2(a,b,c) a.erase(a,b,c)
#define RM(a,b) a.remove(b)
#define IS(a,b) a.insert(b)
#define FD find

#define DI(...) L __VA_ARGS__;I(__VA_ARGS__)
#define DV(a,b) V a;VI(b,a)
#define DIV(a,b) DI(a);DV(b,a)
#define DIV2(a,b,c) DI(a);V b,c;VI2(a,b,c)
#define DI2V(a,b,c) DI(a,b);V2(c,a,b);HI2(c)
#define PI(a) PB(a,IN())
#define SO(a,b,c) O(a?#b:#c)
#define OutSet(a,b) K=a,E=b

#define HI(a) FA(X,a)I(X)
#define HI2(a) FA2(Y,a)I(Y)

#define HO(a) J=1;FE(X,a){if(J){cout<<X;J=0;}else cout<<K<<X;}cout<<E
#define HO2(a) FE(Y,a){J=1;FE(X,Y){if(J){cout<<X;J=0;}else cout<<K<<X;}cout<<E;}

#define VI(a,b) R(I,a)PI(b)
#define VI2(a,b,c) VI(a,b),PI(c)
#define VI3(a,b,c,d) VI2(a,b,c),PI(d)
#define VI4(a,b,c,d,e) VI3(a,b,c,d),PI(e)

#define VO(a) SA=SZ(a);R(i,SA)O(a[i])
#define VO2(a,b) SA=SZ(a);R(i,SA)O(a[i],b[i])
#define VO3(a,b,c) SA=VZ(a);R(i,SA)O(a[i],b[i],c[i])
#define VO4(a,b,c,d) SA=VZ(a);R(i,SA)O(a[i],b[i],c[i],d[i])

#define TypeSet(a) if(#a[0]=='A')U=0;elif(#a[0]=='L')U=2;else U=1

#define AZ(a) L(sizeof(a)/sizeof(a[0]))
#define VZ(a) L(a.size())
#define SZ(a) (U?VZ(a):AZ(a))

#define AT(a) a,a+AZ(a)
#define VT(a) a.begin(),a.end()
#define IT(a) (U?VT(a):AT(a))

#define AX(a) *max_element(AT(a))
#define AN(a) *min_element(AT(a))
#define VX(a) *max_element(VT(a))
#define VN(a) *min_element(VT(a))
#define MX(a) (U?VX(a):AX(a))
#define MN(a) (U?VN(a):AN(a))

#define AB(a,b) binary_search(AT(a),b)
#define VB(a,b) binary_search(VT(a),b)
#define BS(a,b) (U?VB(a,b):AB(a,b))

#define AA(a) accumulate(AT(a),0)
#define VA(a) accumulate(VT(a),0)
#define AC(a) (U?VA(a):AA(a))

#define AF(a,b) FD(AT(a),b)
#define VF(a,b) FD(VT(a),b)
#define FN(a,b) (U?VF(a,b):AF(a,b))

#define AS(a) sort(AT(a))
#define VS(a) sort(VT(a))
#define LS(a) a.sort()
#define ST(a) (U-1?(U-2?AS(a):LS(a)):VS(a))

#define P(a) cout<<#a<<N
#define F10(a) cout<<fixed<<setprecision(10)<<a
#define FC(a,b,c) cout<<setfill(a)<<setw(b)<<right<<c

#define Q cin.tie(0);ios::sync_with_stdio(false)
#define LCM(a,b) a*b/GCD(a,b)
#define MXZ(a,b) a=max(a,b)
#define MNZ(a,b) a=min(a,b)

CO L P[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
CH K=S,E=N;
BL J;
L U,SA;

IV I(){}TTA IV I(Tt2,Aa2){cin>>t;I(M(a));}
TT IV O(Tt){cout<<t<<E;}TTA IV O(Tt,Aa){cout<<t<<K;O(M(a));}

TL T IN(){T x;I(x);RT(x);}

def DIG(L a){L d=1;W(a/=10)++d;RT d;}
def GCD(L a,L b){L c;W(a){c=a;a=b%a;b=c;}RT b;}
IB ISP(L a){
    if(a<48){F(i,0,15)if(P[i]==a)RT 1;}
    elif(a<2809)RT GCD(614889782588491410,a)==1;
    else {P(Error:Argument>2808);exit(1);}RT 0;}

class comp{public:CO V&u;
    comp(CO V&v):u(v){}
    bool operator()(L a,L b){RT u[a]<u[b];}};
def S2V(V&a,V&b){
    L n=VZ(a);V p(n),a2(n),b2(n);R(i,n)p[i]=i;
    sort(VT(p),comp(a));
    R(i,n)a2[i]=a[p[i]],b2[i]=b[p[i]];a=a2,b=b2;
}
def VVS(VV&a,L b){
    L m=VZ(a),n=VZ(a[0]);V p(n);V2(a2,m,n);
    if(b>=m||b<0){P(Error:Out of Range);exit(1);}
    R(i,n)p[i]=i;sort(VT(p),comp(a[b]));
    R(i,m)R(j,n)a2[i][j]=a[i][p[j]];
    a=a2;
}

def AFD(V a,L c){
    V b;
    R(i,VZ(a))if(a[i]==c)PB(b,i);
    RT b;
}

def VRS(V a){
    V b(VZ(a));
    b[0]=a[0];
    F(i,1,VZ(a))b[i]=a[i]+b[i-1];
    RT b;
}
def RS(L*a,L*b){
    b[0]=a[0];
    F(i,1,AZ(a))b[i]=a[i]+b[i-1];
}

def MG(V&a,L li=0,L mi=-1,L ri=-1){
    if(ri==-1)ri=VZ(a);if(mi==-1)mi=ri/2;
    L ln=mi-li,rn=ri-mi,i=0,j=0;V l,r;
    R(k,ln)PB(l,a[li+k]);
    R(k,rn)PB(r,a[mi+k]);
    PB(l,INF),PB(r,INF);
    F(k,li,ri)a[k]=l[i]<r[j]?l[i++]:r[j++];
}
IV MS(V&a,L li=0,L ri=-1){
    if(ri==-1)ri=VZ(a);
    if(ri-li>1){L mi=(li+ri)/2;
        MS(a,li,mi),MS(a,mi,ri),MG(a,li,mi,ri);}
}
def PT(V&a,L li=0,L ri=-1){
    if(ri==-1)ri=VZ(a);L p=a[ri-1];L ln=li;
    F(i,li,ri)if(a[i]<=p)swap(a[i],a[ln++]);
    RT ln-1;
}
IV QS(V&a,L li=0,L ri=-1){
    if(ri==-1)ri=VZ(a);
    if(ri-li>1){L mi=(li+ri)/2;swap(a[ri-1],a[mi]);
        L p=PT(a,li,ri);QS(a,li,p),QS(a,p+1,ri);}
}

def perm(void a(V),V b){VS(b);do{a(b);}W(next_permutation(VT(b)));}
//in main: perm([](V a){FP(a);},A);
L a,b,c,d=1,M=MOD;STR A;
int main(){
    Q;TypeSet(V);
    I(A);
    FE(i,A){
        if(i=='A')a=(a+d)%M;
        elif(i=='B')b=(b+a)%M;
        elif(i=='C')c=(c+b)%M;
        else{
            c=(c*3+b)%M;
            b=(b*3+a)%M;
            a=(a*3+d)%M;
            d=(d*3)%M;
        }
    }
    OutSet(S,N);
    O(c);
}

//for compilation: g++ -o _ _.cpp -std=c++14
//
//to find error: g++ -g -o _ _.cpp -std=c++14
//gdb _.out
//run
//
//for fastening: g++ -O3 -mtune=native -march=native -o _ _.cpp -std=c++14


Measured memory_bytes: 512