Language: C++

#include <iostream>
#include <algorithm>
#include <vector>
#include <complex>

using namespace std;

const long waru = 1000000007;
long pow3[100010];

int main() {
    string s;
    cin >> s;

    pow3[0] = 1;
    for(int i=1; i<pow3.length(); i++)
    {
        pow3[i] = pow3[i-1] * 3 % waru;
    }

    long a[100001] = {};
    long c[100001] = {};
    long h[100001] = {};
    for(int i=0; i<s.length(); i++)
    {
        if(i != 0)
        {
            a[i] += a[i-1];
            c[i] += c[i-1];
            h[i] += h[i-1];
        }

        if(s[i] == 'A') a[i]++;
        if(s[i] == 'C') c[i]++;
        if(s[i] == '?') h[i]++;
    }

    long ans = 0;

    for(int i=1; i<s.length()-1; i++)
    {
        if(s[i] == 'B' || s[i] == '?')
        {
            int lh = h[s.length()-1] - h[i];
            int lc = c[s.length()-1] - c[i];

            // 1
            if(a[i-1] > 0 && lc > 0)
            {
                ans = (ans + a[i-1] * lc * pow3[h[i-1] + lh)] % waru;
            }

            // 2
            if(h[i-1] > 0 && lc > 0)
            {
                ans = (ans + h[i-1] * lc * pow3[h[i-1] - 1 + lh)] % waru;
            }

            // 3
            if(a[i-1] > 0 && lh > 0)
            {
                ans = (ans + a[i-1] * lh * pow3[h[i-1] + lh - 1)] % waru;
            }

            // 4
            if(h[i-1] > 0 && lh > 0)
            {
                ans = (ans + h[i-1] * lh * pow3[h[i-1] - 1 + lh - 1)] % waru;
            }
        }
    }
    
    cout << ans << endl;

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:16:25: error: request for member 'length' in 'pow3', which is of non-class type 'long int [100010]'
   16 |     for(int i=1; i<pow3.length(); i++)
      |                         ^~~~~~
a.cc:50:60: error: expected ']' before ')' token
   50 |                 ans = (ans + a[i-1] * lc * pow3[h[i-1] + lh)] % waru;
      |                                                            ^
      |                                                            ]
a.cc:50:61: error: expected ';' before ']' token
   50 |                 ans = (ans + a[i-1] * lc * pow3[h[i-1] + lh)] % waru;
      |                                                             ^
      |                                                             ;
a.cc:56:64: error: expected ']' before ')' token
   56 |                 ans = (ans + h[i-1] * lc * pow3[h[i-1] - 1 + lh)] % waru;
      |                                                                ^
      |                                                                ]
a.cc:56:65: error: expected ';' before ']' token
   56 |                 ans = (ans + h[i-1] * lc * pow3[h[i-1] - 1 + lh)] % waru;
      |                                                                 ^
      |                                                                 ;
a.cc:62:64: error: expected ']' before ')' token
   62 |                 ans = (ans + a[i-1] * lh * pow3[h[i-1] + lh - 1)] % waru;
      |                                                                ^
      |                                                                ]
a.cc:62:65: error: expected ';' before ']' token
   62 |                 ans = (ans + a[i-1] * lh * pow3[h[i-1] + lh - 1)] % waru;
      |                                                                 ^
      |                                                                 ;
a.cc:68:68: error: expected ']' before ')' token
   68 |                 ans = (ans + h[i-1] * lh * pow3[h[i-1] - 1 + lh - 1)] % waru;
      |                                                                    ^
      |                                                                    ]
a.cc:68:69: error: expected ';' before ']' token
   68 |                 ans = (ans + h[i-1] * lh * pow3[h[i-1] - 1 + lh - 1)] % waru;
      |                                                                     ^
      |                                                                     ;