stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h> 

using namespace std;

long long solution(int pos, string s, string cad)

{

	if(pos==cad.size()-1)

	{

		s += cad[pos];

		long long suma=0;

		char array[s.size()];

		strcpy(array,s.c_str());

		char *ella=strtok(array,"+");

		while(ella)

		{

			string cambio(ella);

			long long n=atoll(cambio.c_str());

			suma+=n;

			ella=strtok(NULL,"+");

		}

		return suma;



	} else {

		return solution(pos+1,s+cad[pos]+'+',cad)+

		solution(pos+1,s+cad[pos],cad);

	}

}

main()

{

	string cad;

	cin>>cad;

	string s="";

	long long a=solution(0,s,cad);

	cout<<a<<endl;

}




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

#include <cstring>

char s[32];

int len,tong[32];

long long answer;

void charge(int dep,long long num,int cnt) {

	if(dep==len) {

		long long ans=num;

	//	printf("%d ",num);

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

			ans+=tong[i];

		//	printf("%d ",tong[i]);

		}

		//printf("\n");

		answer+=ans;

		return ;

	}

	tong[cnt+1]=num;

	charge(dep+1,s[dep]-'0',cnt+1);

	tong[cnt+1]=0;

	charge(dep+1,num*10+s[dep]-'0',cnt);

}

int main() {

	scanf("%s",s);

	len=strlen(s);

	answer=0;

	charge(0,0,0);

	printf("%lld\n",answer/2);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

string s;

long long ans;

vector<int> p;

long long to_num(string x)

{

	long long n=0;

	while(x.size())

	{

		n*=10;

		n+=x[0]-'0';

		x=x.substr(1);

	}

	return n; 

}

void dfs(int n)

{

	if(n==s.size())

	{

		p.push_back(n);

		for(int i=1;i<p.size();i++)ans+=to_num(s.substr(p[i-1],p[i]-p[i-1]));

		p.erase(p.end()-1);

		return;

	}

	dfs(n+1);

	p.push_back(n+1);

	dfs(n+1);

	p.erase(p.end()-1);

}

int main()

{

	cin>>s;

	p.push_back(0);

	dfs(0);

	cout<<ans/2;

	return 0;

}

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

#include<cstring>

using namespace std;

typedef long long ll;

const int MAXN=9+3;

char s[MAXN];

int n;

ll ans;

int main(){

	scanf("%s",&s);

	n=strlen(s);

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

		int last=-1;

		ll res=0;

		for(int j=0;j<n-1;j++){

			if((i&1<<j)!=0){

				ll cur=0;

				for(int k=last+1;k<=j;k++){

					cur=cur*10+s[k]-'0';

				}

				res+=cur;

				last=j;

			}

		}

		ll cur=0;

		for(int j=last+1;j<n;j++){

			cur=cur*10+s[j]-'0';

		}

		res+=cur;

		ans+=res;

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<stdlib.h>

#include<stack>

using namespace std;

const int N=1e4+50;

const int INF=0x3f3f3f;

char s[N];

int vis[N];

long long num(int l,int r)

{

    long long sum=0;

    for(int i=l;i<=r;i++){

        sum=sum*10+s[i]-'0';

    }

    return sum;

}

int main()

{

    scanf("%s",s);

    int len=strlen(s);

    long long sum=0;

    int tep= (1<<(len-1))-1;

    for(int i=0;i<=tep;i++)

    {

        memset(vis,0,sizeof(vis));

        int k=0;

        int q=i;

        while(q)

        {

            vis[k++]=q%2;

            q/=2;

        }

        int l=-1,r=0;

        vis[len-1]=1;

       for(int i=0;i<len;i++)

        {

          if(vis[i]==0)

           r++;

          else

          {

            sum+=num(l+1,i);

            l=i;

          }

        }

    }

    printf("%lld\n",sum);

    return 0;

}

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

#include<cstring>

using namespace std;

#define MAXN 16

#define LL long long

char s[MAXN];

int digit[MAXN];

LL ans,N;

LL Pow(int x)

{

	LL res=1;

	while(x--)

		res*=10;

	return res;

}

int Abs(int x)

{

	if(x>0) return x;

		else return -x;

}

void dfs(int pos,LL sum,LL last)

{

	if(pos>N)

	{

		ans=ans+sum+last;

		return ;

	}

	dfs(pos+1,sum,last*10+digit[pos]);

	dfs(pos+1,sum+last,digit[pos]);

}

int main()

{

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

	N=strlen(s+1);

	for(int i=1;i<=N;i++)

		digit[i]=s[i]-'0';

	dfs(2,0,digit[1]);

	//注意第一种情况确定，第一个数前面不能加'+'，第一位一定是第一个加数的第一位

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



int vis[11];

char a[11];

ll sum;



void digui(int x, int t, int y, int m){

    if(x == y){

     //   ll sum1 = sum;

     //   printf("%lld", sum1);

        ll s = 0;

        for(int i = 0; i < m; i++){

            s = s*10+a[i]-'0';

            if(vis[i]){

                sum += s;

                s = 0;

            }

        }

        sum += s;

     //   printf("  %lld\n", sum-sum1);

        return ;

    }

    for(int i = t; i < m-1; i++){

        if(!vis[i]){

            vis[i] = 1;

           // printf("%d %d %d %d\n", i, x, y, m);

            digui(x+1, i+1, y, m);

            vis[i] = 0;

        }

    }

}



int main(){

    while(~scanf("%s", a)){

        sum = 0;

        int sa = strlen(a);

        for(int i = 0; i < sa-1; i++){

            memset(vis, 0 ,sizeof(vis));

            digui(0,0,i+1, sa);

        }

        ll s = 0;

        for(int i = 0; i < sa; i++) s = s*10+a[i]-'0';

        printf("%lld\n", sum+s);

    }

    return 0;

}


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

#include<cstring>

using namespace std;

#define MAXN 16

#define LL long long

char s[MAXN];

int digit[MAXN];

LL ans,N;

LL Pow(int x)

{

	LL res=1;

	while(x--)

		res*=10;

	return res;

}

int Abs(int x)

{

	if(x>0) return x;

		else return -x;

}

void dfs(int pos,LL sum,LL last)

{

	if(pos>N)

	{

		ans=ans+sum+last;

		return ;

	}

	dfs(pos+1,sum,last*10+digit[pos]);

	dfs(pos+1,sum+last,digit[pos]);

}

int main()

{

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

	N=strlen(s+1);

	for(int i=1;i<=N;i++)

		digit[i]=s[i]-'0';

	dfs(2,0,digit[1]);

	//注意第一种情况确定，第一个数前面不能加'+'，第一位一定是第一个加数的第一位

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<stdlib.h>

#include<stack>

using namespace std;

const int N=1e4+50;

const int INF=0x3f3f3f;

char s[N];

int vis[N];

long long num(int l,int r)

{

    long long sum=0;

    for(int i=l;i<=r;i++){

        sum=sum*10+s[i]-'0';

    }

    return sum;

}

int main()

{

    scanf("%s",s);

    int len=strlen(s);

    long long sum=0;

    int tep= (1<<(len-1))-1;

    for(int i=0;i<=tep;i++)

    {

        memset(vis,0,sizeof(vis));

        int k=0;

        int q=i;

        while(q)

        {

            vis[k++]=q%2;

            q/=2;

        }

        int l=-1,r=0;

        vis[len-1]=1;

       for(int i=0;i<len;i++)

        {

          if(vis[i]==0)

           r++;

          else

          {

            sum+=num(l+1,i);

            l=i;

          }

        }

    }

    printf("%lld\n",sum);

    return 0;

}

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

#include<cstring>

#define ll long long

char str[15];

int len;

ll ans=0;

ll change(int i,int j)

{

    ll sum=0;

    while(i<=j)

    {

        sum=sum*10+(str[i]-'0');

        i++;

    }

    return sum;

}

int fun(int a,int b)

{

    if(a==len-1)

    {

        ans+=str[a]-'0';

        return 1;

    }

    if(a>len-1)

        return 0;

    int num,sum;

    num=sum=0;

    for(int i=a;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(a,i)*num;

        sum+=num;

    }

    ans+=change(a,b);

    return sum+1;

}

int main()

{

    scanf("%s",str);

    len=strlen(str);

    int num;

    for(int i=0;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(0,i)*num;

    }

    ans+=change(0,len-1);

    printf("%lld\n",ans);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



int vis[11];

char a[11];

ll sum;



void digui(int x, int t, int y, int m){

    if(x == y){

     //   ll sum1 = sum;

     //   printf("%lld", sum1);

        ll s = 0;

        for(int i = 0; i < m; i++){

            s = s*10+a[i]-'0';

            if(vis[i]){

                sum += s;

                s = 0;

            }

        }

        sum += s;

     //   printf("  %lld\n", sum-sum1);

        return ;

    }

    for(int i = t; i < m-1; i++){

        if(!vis[i]){

            vis[i] = 1;

           // printf("%d %d %d %d\n", i, x, y, m);

            digui(x+1, i+1, y, m);

            vis[i] = 0;

        }

    }

}



int main(){

    while(~scanf("%s", a)){

        sum = 0;

        int sa = strlen(a);

        for(int i = 0; i < sa-1; i++){

            memset(vis, 0 ,sizeof(vis));

            digui(0,0,i+1, sa);

        }

        ll s = 0;

        for(int i = 0; i < sa; i++) s = s*10+a[i]-'0';

        printf("%lld\n", sum+s);

    }

    return 0;

}


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

#include<cstring>

#define ll long long

char str[15];

int len;

ll ans=0;

ll change(int i,int j)

{

    ll sum=0;

    while(i<=j)

    {

        sum=sum*10+(str[i]-'0');

        i++;

    }

    return sum;

}

int fun(int a,int b)

{

    if(a==len-1)

    {

        ans+=str[a]-'0';

        return 1;

    }

    if(a>len-1)

        return 0;

    int num,sum;

    num=sum=0;

    for(int i=a;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(a,i)*num;

        sum+=num;

    }

    ans+=change(a,b);

    return sum+1;

}

int main()

{

    scanf("%s",str);

    len=strlen(str);

    int num;

    for(int i=0;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(0,i)*num;

    }

    ans+=change(0,len-1);

    printf("%lld\n",ans);

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<cstring>

using namespace std;

#define LL long long

char str[15];

LL ans;

void dfs(LL sum,LL pos,LL len){

	if(pos==len){

		ans+=sum;

		return;

	}

	for(LL i=pos;i<len;++i){

		LL tmp=0;

		for(LL j=pos;j<=i;++j)

			tmp=tmp*10+(str[j]-'0');

		dfs(sum+tmp,i+1,len);

	}

	return;

}

int main(){

	cin>>str;

	int len=strlen(str);

	dfs(0,0,len);

	cout<<ans;

	return 0;

}


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

#include<cstring>

#define ll long long

char str[15];

int len;

ll ans=0;

ll change(int i,int j)

{

    ll sum=0;

    while(i<=j)

    {

        sum=sum*10+(str[i]-'0');

        i++;

    }

    return sum;

}

int fun(int a,int b)

{

    if(a==len-1)

    {

        ans+=str[a]-'0';

        return 1;

    }

    if(a>len-1)

        return 0;

    int num,sum;

    num=sum=0;

    for(int i=a;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(a,i)*num;

        sum+=num;

    }

    ans+=change(a,b);

    return sum+1;

}

int main()

{

    scanf("%s",str);

    len=strlen(str);

    int num;

    for(int i=0;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(0,i)*num;

    }

    ans+=change(0,len-1);

    printf("%lld\n",ans);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



long long eval(string &s) {

	string at = "";

	long long ans = 0;

	for (int i = 0; i < (int)s.size(); ++i) {

		if(s[i] == '+') {

			ans += stoll(at);

			at = "";

		} else {

			at += s[i];

		}

	}

	if(at != "") {

		ans += stoll(at);

	}

	return ans;

}



int main() {

	string s;

	cin >> s;

	int len = s.size();

	long long ans = 0;

	for (int msk = 0; msk < (1<<(len-1)); ++msk) {

		vector <int> plus;

		for (int i = 0; i < len-1; ++i) {

			if(msk & (1<<i)) {

				plus.push_back(i+1);

			}

		}

		int j = 0;

		string at = "";

		for (int i = 0; i < len; ++i) {

			if(j < plus.size() && plus[j] == i) {

				at += "+";

				j++;

			}

			at += s[i];

		}

		//cout << at << endl;

		ans += eval(at);

	}

	cout << ans << endl;

}

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

#include<cstring>

#define ll long long

char str[15];

int len;

ll ans=0;

ll change(int i,int j)

{

    ll sum=0;

    while(i<=j)

    {

        sum=sum*10+(str[i]-'0');

        i++;

    }

    return sum;

}

int fun(int a,int b)

{

    if(a==len-1)

    {

        ans+=str[a]-'0';

        return 1;

    }

    if(a>len-1)

        return 0;

    int num,sum;

    num=sum=0;

    for(int i=a;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(a,i)*num;

        sum+=num;

    }

    ans+=change(a,b);

    return sum+1;

}

int main()

{

    scanf("%s",str);

    len=strlen(str);

    int num;

    for(int i=0;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(0,i)*num;

    }

    ans+=change(0,len-1);

    printf("%lld\n",ans);

    return 0;

}
// Slower C++ solution:
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;

template <typename T> using iit = istream_iterator<T>;

template <typename T> using oit = ostream_iterator<T>;

template <typename T> void write(ostream& out, T t) {

    out << t << '\n';

}

template <typename T, typename... Args>

void write(ostream& out, T t, Args... args) {

    out << t << ' ';

    write(out, args...);

}



int main() {

    ios::sync_with_stdio(false);

    cin.tie(0);



    string s;

    cin >> s;

    int n = s.size();

    vector<pair<ll, ll>> S(1 + n);

    S[n] = {1, 0};

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

        ll d = 0;

        ll cnt = 0, sum = 0;

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

            d *= 10;

            d += (s[j - 1] - '0');



            ll rcnt, rsum;

            tie(rcnt, rsum) = S[j];



            sum += d * rcnt + rsum;

            cnt += rcnt;

        }

        S[i] = {cnt, sum};

    }

    cout << S[0].second << "\n";

}

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

#include<cstring>

#define ll long long

char str[15];

int len;

ll ans=0;

ll change(int i,int j)

{

    ll sum=0;

    while(i<=j)

    {

        sum=sum*10+(str[i]-'0');

        i++;

    }

    return sum;

}

int fun(int a,int b)

{

    if(a==len-1)

    {

        ans+=str[a]-'0';

        return 1;

    }

    if(a>len-1)

        return 0;

    int num,sum;

    num=sum=0;

    for(int i=a;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(a,i)*num;

        sum+=num;

    }

    ans+=change(a,b);

    return sum+1;

}

int main()

{

    scanf("%s",str);

    len=strlen(str);

    int num;

    for(int i=0;i<len;i++)

    {

        num=fun(i+1,len-1);

        ans=ans+change(0,i)*num;

    }

    ans+=change(0,len-1);

    printf("%lld\n",ans);

    return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <cstdlib>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <vector>

#include <map>

#include <cmath>

 

#define rep(i,n) for(int i = 0;i < (n);i++)

#define all(x) (x).begin(),(x).end()

#define pb(x) push_back(x)

#define mp(a,b) make_pair(a,b)



using namespace std;



typedef long long int ll;

typedef vector<int> vi;

typedef vector<vi> vvi;

typedef vector<ll> vl;

typedef vector<pair<int, int> > vpi;

typedef vector<pair<ll, ll> > vpl;



vi bin;

string s;

void binary(int num){

  rep(i, (int)s.size()-1){

    bin[i] = num%2;

    num /= 2;

  }

}



int main(){

	cin.tie(0);

	ios::sync_with_stdio(false);

  cin >> s;

  bin = vi((int)s.size()-1);

  ll ans = 0;

  ll tmp = 0;

  ll lim = pow(2,(int)s.size()-1);

  rep(i, lim){

    rep(j,(int)s.size()){

      if(j == (int)s.size()-1){

        if(tmp > 0){

          ans += tmp*10 + s[j] - '0';

          tmp = 0;

        }else{

          ans += s[j]-'0';

        }

        break;

      }

      if(bin[j]==1){

        if(tmp > 0){

          ans += tmp*10 + s[j] - '0';

          tmp = 0;

        }else{

          ans += s[j] - '0';

        }

      }else{

        tmp *= 10;

        tmp += s[j] - '0';

      }

    }

    binary(i+1);

  }

  cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.353034643518294x):
#include <stdio.h>



char s[11];

typedef long long ll;

ll res = 0;

int p[11], sz=0;



void solve(int a, ll b, ll c) {

  //printf("%d %lld %lld\n", a, b, c);

  if (!s[a]) {

    res += b+c;

    return;

  }



  solve(a+1, b+c, s[a]-'0');

  solve(a+1, b, (s[a]-'0')+c*10);

}



int main(void) {

  scanf("%s", s);



  solve(0, 0, 0);



  printf("%lld\n", res/2);



  return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

using namespace std;

char a[100009];

unsigned long long dp1[100009];

unsigned long long dp3[100009];

unsigned long long dp2[100009];

unsigned long long sum=0;

unsigned long long wenhao=1;

#define mood(x)  ((x)%1000000007) 

int main()

{

	ios::sync_with_stdio(false);

	cin>>(a+1);

	int len=strlen(a+1);

	

	for(int i=1;i<=len;i++)

	{	dp1[i]=dp1[i-1];

		dp2[i]=dp2[i-1];

		dp3[i]=dp3[i-1];

		if(a[i]=='A')

		{

			dp1[i]+=wenhao;

			dp1[i]=mood(dp1[i]);

		}

		else if(a[i]=='B')

		{

			dp2[i]=mood((mood(dp1[i-1])+mood(dp2[i])));

		}

		else if(a[i]=='C')

		{

			dp3[i]=mood((mood(dp2[i-1])+mood(dp3[i])));

		}

		else if(a[i]=='?')

		{   

		 

			

			dp1[i]=mood(dp1[i-1]*3+wenhao);

			dp2[i]=mood(dp2[i-1]*3+dp1[i-1]);

			dp3[i]=mood(dp3[i-1]*3+dp2[i-1]);

			wenhao*=3;

			wenhao=mood(wenhao);

		}

		

	}

	cout<<mood(dp3[len])<<endl;



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.289645653767965x):
#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
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main()

{

  string S;

  cin >> S;



  int N = S.size();

  long long MOD = 1e9 + 7;



  long long dp[100010][4] = {};



  for (int i = N; i >= 0; i--) {

    for (int j = 3; j >= 0; j--) {

      if (i == N) {

        dp[i][j] = (j == 3 ? 1 : 0);

      } else {

        dp[i][j] = dp[i+1][j] * (S[i] == '?' ? 3LL : 1LL);

        if (j < 3 && (S[i] == "ABC"[j] || S[i] == '?')) {

          dp[i][j] += dp[i+1][j+1];

        }

        dp[i][j] %= MOD;

      }

    }

  }

  cout << dp[0][0] << endl;

  return(0);

}

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

#include <set>

#include <map>

#include <vector>

#include <algorithm>

#include <iomanip>

#include <climits>

#include <numeric>

#include <cmath>

#include <queue>

#include <sstream>



using namespace std;

typedef long long ll;



const int MOD = 1e9 + 7;



int main() {

  string S;

  cin >> S;

  

  ll a = 0, ab = 0, abc = 0, all = 1;

  for (int i = 0; i < S.size(); i++) {

    if (S[i] == 'A') {

      a += all;

    }

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

      ab += a;

    }

    if (S[i] == 'C') {

      abc += ab;

    }

    if (S[i] == '?') {

      abc = abc * 3 + ab;

      ab  = ab * 3 + a;

      a   = a * 3 + all;

      all *= 3;

    }

    abc %= MOD;

    ab  %= MOD;

    a   %= MOD;

    all %= MOD;

  }

  cout << abc << endl;

  return 0;

}
// Slower C++ solution:
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

//#include <algorithm>

#include <vector>



const long MOD = 1000000007;



long long kaijo(long a, long b)

{

    if(b==0) {

        return 1;

    }

    else if(b%2==0) {

        long long tmp = kaijo(a, b/2) % MOD;

        return (tmp * tmp) % MOD; 

    }

    else {

        return (kaijo(a, b-1) * a) % MOD;

    }

}



long long st_calc( long a, long b )

{

    long long tmp = 0;



    tmp += a * kaijo(3, b) % MOD;

    if(b>0) {

        tmp += kaijo(3, b-1) * b;

    }



    return (tmp % MOD);

}



int main(int argc, char* argv[])

{

    char str[100001]={0};

    scanf("%s", str);

    long len = strlen(str);



    std::vector<long> num_a(len+1);

    std::vector<long> num_c(len+1);

    std::vector<long> num_u(len+1);



    long i;

    for(i=0; i<len; i++) {

        if(str[i]=='A')

            num_a[i] ++;

        else if(str[i]=='C')

            num_c[i] ++;

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

            num_u[i] ++;

    }



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

        num_a[i] += num_a[i-1];

        num_c[i] += num_c[i-1];

        num_u[i] += num_u[i-1];

    }



    long long sum = 0;

    for(i=1; i<len-1; i++) {

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

            sum += (long long)st_calc(num_a[i-1], num_u[i-1]) * st_calc(num_c[len-1] - num_c[i], num_u[len-1] - num_u[i]);

            sum %= MOD;

        }

    }



    printf("%lld\n", sum);



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.430186442228493x):
#include <stdio.h>

#include <string>

#include <cstring>

#include <stdlib.h>

#include <math.h>

#include <algorithm>

#include <vector>

#include <set>

#include <map>

#include <queue>

#include <list>

#include <iterator>

#include <assert.h>

#pragma warning(disable:4996) 



typedef long long ll;

#define MIN(a, b) ((a)>(b)? (b): (a))

#define MAX(a, b) ((a)<(b)? (b): (a))

#define LINF 9223300000000000000

#define INF 2140000000

const long long MOD = 1000000007;

using namespace std;



char str[100005];

ll   dp[2][4];   // (curr or next), (empty,A,AB,ABC)



int main(int argc, char* argv[])

{

    scanf("%s", str);

    int len=strlen(str);



    int curr=0, next=1;

    dp[0][curr]=1;

    int i;

    for(i=0; i<len; i++) {

        int k;

        for(k=0; k<4; k++) {

            dp[next][k]=(str[i]=='?'? (dp[curr][k]*3%MOD): dp[curr][k]);

        }

        if(str[i]=='?' || str[i]=='C') dp[next][3]=(dp[next][3]+dp[curr][2])%MOD;            

        if(str[i]=='?' || str[i]=='B') dp[next][2]=(dp[next][2]+dp[curr][1])%MOD;

        if(str[i]=='?' || str[i]=='A') dp[next][1]=(dp[next][1]+dp[curr][0])%MOD;



        swap(curr,next);

    }

    printf("%lld\n", dp[curr][3]);



    return 0;

}