#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <regex>

/* g++ -g -std=c++0x */
/* g++ -g -std=c++11 */

using namespace std;

// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

const int mod = 1e9+7;
const int maxn = 1e5+5;
int power[maxn], dp[maxn][3];
string s;

int mul(const LL a, const LL b)
{ return (a*b % mod + mod ) % mod; }
int mul(const LL a, const LL b, const LL c)
{ return mul(a, mul(b,c)); }
int mul(const LL a, const LL b, const LL c, const LL d)
{ return mul(a, mul(b,c,d)); }

int dfs(const int i, const int j)
{
    if (i < 1 && j > 0)
        return 0;
    if (i == 0)
        return s[i] == 'A' | s[i] == '?';
    int& ans = dp[i][j];
    if (-1 == ans) {
        ans = 0;
        if (s[i] == '?') {
            FOR(_,0,3) {
                ans += dfs(i-1,j); ans %= mod;
            }
            if (j > 0) 
                ans += dfs(i-1,j-1); 
            else
                ans += power[i-1];
            ans %= mod;
        }
        else {
            if (s[i]-'A' == j) {
                if (j > 0)
                    ans += dfs(i-1,j-1);
                else
                    ans += power[i-1];
                ans %= mod;
            }
            ans += dfs(i-1,j); ans %= mod;
        }
    }
    return ans;
}

int main()
{
/* #ifndef ONLINE_JUDGE */
/*     freopen("foo", "rt", stdin); */
/* #endif */
    ios::sync_with_stdio(false);
    while (cin >> s) {
        memset(dp,-1,sizeof(dp));
        const int n = s.size();
        power[0] = s[0] == '?' ? 3 : 1;
        FOR(i,1,n)
            power[i] = 1LL * power[i-1] * (s[i] == '?' ? 3 : 1) % mod;
        const int ans = dfs(n-1,2);
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 8272