stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<stdio.h>

#include<iostream>

#include<math.h>

#include<stdlib.h>

using namespace std;





int main(){

    int a,b;

    cin>>a>>b;

    a--;

    b--;

    cout<<a*b;



    return 0;



}


// Optimized solution to the same problem (faster):
#include<stdio.h>



int main()

{

	int a,b;

	scanf("%d %d",&a,&b);

	printf("%d",(a-1)*(b-1));

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

 

int main(){

    int N;

    cin >> N;

    vector<int> A(N);

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

    {

        cin >> A[i];

    }

 

    long long ans = 0;

    int MaxNum = 0;

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

    {

        //MaxNumを使い回すことで計算を抑える

        MaxNum = max(MaxNum, A[i]);

        ans += MaxNum - A[i];

    }

 

    cout << ans << endl;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>



int main()

{

	long long int a[2*100000+10];

	int i=0,n,max=-1;

	long long int cou=0;

	

	scanf("%d",&n);

	

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

		scanf("%lld",&a[i]);

	}

	

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

		max=(max>a[i])?max:a[i];

		cou+=max-a[i];

	}

	

	printf("%lld",cou);	

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



long long int cclt(long long int n);

long long int c(long long int n);



int main()

{

    long long int n;

    cin>>n;

    long long int rst=0;

    rst=cclt(n);

    cout<<rst<<endl;

    return 0;

}



long long int cclt(long long int n)

{

    if(n==0)

    {

        return 0;

    }

    long long int rst=0,f=1;

    while(n/f>0)

    {

        rst+=cclt(n%f)+(n/f)*c(n%f);

        f*=10;

    }

    return rst;

}



long long int c(long long int n)

{

    int t=1;

    while(n/10>0)

    {

        t*=2;

        n/=10;

    }

    return t;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

#include<string.h>

using namespace std ;

#define N 15

int main()

{

    int n,m=1,i,j;

    char x[15];

    long long sum=0,a;

    scanf("%s",x);

    n=strlen(x);

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

    {

        m=m*2;

    }

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

    {

        a=x[0]-'0';

        for(j=0;j<n;j++)

        {

            if(j==n-1||i&1<<j)

            {

            sum=sum+a;

            a=0;

            if(j==n-1) break;

            }

            a=a*10;

            a=a+x[j+1]-'0';

        }

    }

    printf("%lld",sum);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<stdlib.h>

#include<cmath>

using namespace std;

	long long ans = 0;

void dfs(long long s,long long n)

{

	if (s == 0) ans+=n;

	else {

		int t = log10(s);

		//ans += s;

		while (t >= 0)

		{

			long long a = s / pow(10, t);

			//ans += a+n;

			// cout << a << endl;

			long long b = s - a*pow(10, t);

			dfs(b, a + n);

			t--;

		}

	}

}

int main()

{

	long long s;

	cin >> s;

	dfs(s,0);

	cout << ans;



}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

#include<string.h>

using namespace std ;

#define N 15

int main()

{

    int n,m=1,i,j;

    char x[15];

    long long sum=0,a;

    scanf("%s",x);

    n=strlen(x);

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

    {

        m=m*2;

    }

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

    {

        a=x[0]-'0';

        for(j=0;j<n;j++)

        {

            if(j==n-1||i&1<<j)

            {

            sum=sum+a;

            a=0;

            if(j==n-1) break;

            }

            a=a*10;

            a=a+x[j+1]-'0';

        }

    }

    printf("%lld",sum);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstring>

#include <cmath>

#include <algorithm>

using namespace std;

const int MAXN = 15;

char s[MAXN];

int main() {

    ios::sync_with_stdio(false);

    while(cin >> s){

        int len = strlen(s);

        long long ans = 0;

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

            long long t = s[0] - '0';

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

                if(j == len - 1 || i & (1 << j)){

                    ans += t;

                    t = 0;

                    if(len - 1 == j) 

						break;

                }

                t *= 10;

                t += s[j + 1] - '0';

            }

        }

        cout << ans << endl;

    }



    return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

#include<string.h>

using namespace std ;

#define N 15

int main()

{

    int n,m=1,i,j;

    char x[15];

    long long sum=0,a;

    scanf("%s",x);

    n=strlen(x);

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

    {

        m=m*2;

    }

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

    {

        a=x[0]-'0';

        for(j=0;j<n;j++)

        {

            if(j==n-1||i&1<<j)

            {

            sum=sum+a;

            a=0;

            if(j==n-1) break;

            }

            a=a*10;

            a=a+x[j+1]-'0';

        }

    }

    printf("%lld",sum);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

using namespace std;

    using ll = long long;

    ll num[15][15];

    ll ans=0;

    int n;

void run(int step,ll sum)

{

    if(step==n)

    {

        ans+=sum;

        return;

    }

    for(int i=step;i<n;i++)

        run(i+1,sum+num[step][i]);

    return;

}

int main()

{

    string s;

    cin>>s;

    n=s.size();

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

    {

        string t;

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

        {

            t.push_back(s[j]);

            num[i][j]=stoll(t);

        }

    }

    run(0,0);

    cout<<ans<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<algorithm>

#include<string.h>

using namespace std ;

#define N 15

int main()

{

    int n,m=1,i,j;

    char x[15];

    long long sum=0,a;

    scanf("%s",x);

    n=strlen(x);

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

    {

        m=m*2;

    }

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

    {

        a=x[0]-'0';

        for(j=0;j<n;j++)

        {

            if(j==n-1||i&1<<j)

            {

            sum=sum+a;

            a=0;

            if(j==n-1) break;

            }

            a=a*10;

            a=a+x[j+1]-'0';

        }

    }

    printf("%lld",sum);

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



string str;

int n;



long long solve(vector <int> a) {

	int pos = 0;	

	long long ans = 0;

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

		long long sum = 0;

		for (int j = pos; j <= a[i]; j++) {

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

		}	

		pos = a[i] + 1;

		ans += sum;

	}

	long long sum = 0;

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

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

	}

	ans += sum;

	return ans;

}



int main() {

	cin >> str;

	n = str.size();

	long long ans = 0;

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

		vector <int> pos;

		int cnt = 0;

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

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

				pos.push_back(j);

				cnt++;									

			}	

		}  

		ans += solve(pos);

	}

	cout << ans << endl;

	return 0;

}                     

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}




// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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";

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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


// Fastest known solution for this problem: