Problem:
Score : 400 points

Problem Statement

The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:

1 ≤ i < j < k ≤ |T| (|T| is the length of T.)

T_i = A (T_i is the i-th character of T from the beginning.)

T_j = B

T_k = C

For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.

You are given a string S. Each character of S is A, B, C or ?.

Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.

This sum can be extremely large, so print the sum modulo 10^9 + 7.

Constraints

3 ≤ |S| ≤ 10^5

Each character of S is A, B, C or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

Sample Input 1

A??C

Sample Output 1

8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:

AAAC: 0

AABC: 2

AACC: 0

ABAC: 1

ABBC: 2

ABCC: 2

ACAC: 0

ACBC: 1

ACCC: 0

The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Sample Input 2

ABCBC

Sample Output 2

3

When Q = 0, we print the ABC number of S itself, modulo 10^9 + 7. This string is the same as the one given as an example in the problem statement, and its ABC number is 3.

Sample Input 3

????C?????B??????A???????

Sample Output 3

979596887

In this case, the sum of the ABC numbers of all the 3^Q strings is 2291979612924, and we should print this number modulo 10^9 + 7, that is, 979596887.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>

#include <vector>
#include <list>
#include<stack>
#include<queue>
#include<array>

#include <set>
#include<map>

#include<string>
#include<stdlib.h>

#include<algorithm>
#include <functional>
#include<math.h>

#include<fstream>
#include<iomanip>

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;

#define FOR(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<<str<<endl

constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 60);
constexpr ll MOD = 1000000007ll;// *1000000007ll;// 10^9+7


//変数
string s;






//サブ関数
//入力
void input()
{
	cin >> s;
}



//繰り返し累乗法
//in:  x,n,mod
//out: x^n(Mod mod)
template<typename T>
T mod_pow(T x, T n, T mod) {
	T res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}

map<char, vector<ll>> accumu(string s)
{
	map<char, vector<ll>> res;
	res['A'] = res['B'] = res['C'] = res['?'] = vector<ll>(s.size());
	REP(i, s.size()) {
		res[s[i]][i] = 1;
	}
	for (auto& row : res) {
		for (int i = s.size() - 1; i > 0; i--) {
			row.second[i - 1] += row.second[i];
		}
	}
	return res;
}

// k を決定したときに満たすのは、何通り？
//( 決定できるそれぞれに対し、後ろの?の個数だけ場合がある )
vector<ll> define_c(const string& s, map<char,vector<ll>> acc)
{
	vector<ll> res(s.size(), 0);
	REP(i, s.size()) if (s[i] == 'C' || s[i] == '?') {
		if (i + 1 == s.size())res[i] = 1;
		else res[i] = mod_pow(3ll, acc['?'][i + 1], MOD);
	}
	for (int i = s.size() - 1; i > 0; i--) {
		res[i - 1] += res[i] * (s[i - 1] == '?' ? 3 : 1);
		res[i - 1] %= MOD;
	}
	return res;
}

vector<ll> define_a(const string& s, map<char,vector<ll>> acc)
{
	vector<ll> res(s.size(), 0);
	REP(i, s.size())if (s[i] == 'A' || s[i] == '?') {
		if (i == 0)res[i] = 1;
		else res[i] = mod_pow(3ll, acc['?'][0] - acc['?'][i], MOD);
	}
	REP(i, s.size() - 1) {
		res[i + 1] += res[i] * (s[i + 1] == '?' ? 3 : 1);
		res[i + 1] %= MOD;
	}
	return res;
}

//計算
void calc()
{
	ll res = 0;
	auto acc = accumu(s);
	auto numc = define_c(s, acc);
	auto numa = define_a(s, acc);

	FOR(i, 1, s.size() - 1) if (s[i] == 'B' || s[i] == '?') {
		res += numa[i - 1] * numc[i + 1];
		res %= MOD;
	}
	cout << res << endl;
}


//出力
void output()
{

}


//デバッグ
void debug()
{
	int N;
	cin>>N;
}


//メイン関数
int main()
{
	input();
	calc();
	output();
	debug();
	
	return 0;
}