// Slower C++ solution:
#include<bits/stdc++.h> //Ithea Myse Valgulious

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rec register char

#define rel register ll

#define gc getchar

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return 0&pc(48);

  if (x<0) x=-x,pc('-');

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=1e5,mod=1e9+7;

typedef ll fuko[yuzu|10];

ll n,ans;

fuko a,c,wh,wq;

char str[yuzu|10];

 

ll kasumi(ll a,ll b){

if (b<0) return 0;

ll s=1;

for (;b;b>>=1,a=a*a%mod) if (b&1) s=s*a%mod;

return s;

}

 

int main(){

int i,j;

scanf("%s",str+1);

n=strlen(str+1);

for (i=1;i<=n;i++){

  a[i]=a[i-1]+(str[i]=='A');

  wq[i]=wq[i-1]+(str[i]=='?');

  }

for (i=n;i;i--){

  c[i]=c[i+1]+(str[i]=='C');

  wh[i]=wh[i+1]+(str[i]=='?');

  }

for (i=1;i<=n;i++){

  if (str[i]=='B'||str[i]=='?'){

    ans=(ans+(a[i-1]*kasumi(3,wq[i-1])%mod+kasumi(3,wq[i-1]-1)*wq[i-1]%mod)%mod*(c[i+1]*kasumi(3,wh[i+1])%mod+kasumi(3,wh[i+1]-1)*wh[i+1]%mod)%mod)%mod;

    }

  }

write(ans);

}

// Faster C++ solution for the same problem (measured speedup: 5.155442943510277x):
#include <algorithm>

#include <cstring>

#include <cstdio>

using namespace std;



const int N = 1e5 + 10;

const int mo = 1e9 + 7;



char str[N];

int n, pw[N] = {1}, pre[N], suf[N], p[N], s[N];



int main() {

	scanf("%s", str + 1);

	for(int i = 1; i <= 1000; i ++) i = i - 1 + 1;

	n = strlen(str + 1);

	for(int i = 1; i <= n; i ++) { pw[i] = pw[i - 1] * 3ll % mo; }

	for(int i = 1; i <= n; i ++) {

		pre[i] = pre[i - 1] + (str[i] == 'A');

		p[i] = p[i - 1] + (str[i] == '?');

	}

	for(int i = n; i >= 1; i --) {

		suf[i] = suf[i + 1] + (str[i] == 'C');

		s[i] = s[i + 1] + (str[i] == '?');

	}

	int ans = 0;

	for(int i = 1; i <= n; i ++) {

		if(str[i] == 'B') {

			(ans += 1ll * pre[i - 1] * suf[i + 1] % mo * pw[p[n]] % mo) %= mo;

			if(p[i]) (ans += 1ll * p[i] % mo * suf[i + 1] % mo * pw[p[n] - 1] % mo) %= mo;

			if(s[i]) (ans += 1ll * s[i] % mo * pre[i - 1] % mo * pw[p[n] - 1] % mo) %= mo;

			if(p[i] && s[i])

				(ans += 1ll * s[i] * p[i] % mo * pw[p[n] - 2] % mo) %= mo;

		} else if(str[i] == '?') {

			(ans += 1ll * pre[i - 1] * suf[i + 1] % mo * pw[p[n] - 1] % mo) %= mo;

			if(p[i - 1]) (ans += 1ll * p[i - 1] % mo * suf[i + 1] % mo * pw[p[n] - 2] % mo) %= mo;

			if(s[i + 1]) (ans += 1ll * s[i + 1] % mo * pre[i - 1] % mo * pw[p[n] - 2] % mo) %= mo;

			if(p[i - 1] && s[i + 1])

				(ans += 1ll * s[i + 1] * p[i - 1] % mo * pw[p[n] - 3] % mo) %= mo;

		}

	}

	printf("%d", ans);

	return 0;

}

//AtCoder - 4232