Language: C++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <numeric>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

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


int main() {

  string s;

  cin >> s;

  ll n = s.size();

  vector<ll> left_a(n), left_q(n), right_c(n), right_q(n);
  // if (s[0] == 'A') {
  //   left_a[0] = 1;
  //   left_q[0] = 0;
  // } else if (s[0] == '?') {
  //   left_a[0] = 0;
  //   left_q[0] = 1;
  // }
  left_a[0] = 0;
  left_q[0] = 0;
  for (ll i = 0; i < n-1; i++) {
    if (s[i] == 'A') {
      left_a[i+1] = left_a[i] + 1;
      left_q[i+1] = left_q[i];
    } else if (s[i] == '?') {
      left_a[i+1] = left_a[i];
      left_q[i+1] = left_q[i] + 1;
    } else {
      left_a[i+1] = left_a[i];
      left_q[i+1] = left_q[i];
    }
  }

  // if (s[n-1] == 'C') {
  //   right_c[n-1] = 1;
  //   right_q[n-1] = 0;
  // } else if (s[n-1] == '?') {
  //   right_c[n-1] = 0;
  //   right_q[n-1] = 1;
  // }
  right_c[n-1] = 0;
  right_q[n-1] = 0;

  for (ll i = n-1; i >= 1; i--){
    if (s[i] == 'C') {
      right_c[i-1] = right_c[i] + 1;
      right_q[i-1] = right_q[i];
    }else if (s[i] == '?') {
      right_c[i-1] = right_c[i];
      right_q[i-1] = right_q[i] + 1;
    }else {
      right_c[i-1] = right_c[i];
      right_q[i-1] = right_q[i];
    }
  }

  // REP(i, n) cout << left_a[i];
  // cout << endl;
  // REP(i, n) cout << left_q[i];
  // cout << endl;
  // REP(i, n) cout << right_c[i];
  // cout << endl;
  // REP(i, n) cout << right_q[i];
  // cout << endl;

  ll M = pow(10, 9) + 7;
  ll ans = 0;

  for (ll i = 1; i < n-1; i++) {
    if (s[i] == 'B' || s[i] == '?') {
      ans += ((left_a[i] * pow(3, left_q[i]) + left_q[i] * long(pow(3, left_q[i] - 1))) * (right_c[i] * pow(3, right_q[i]) + right_q[i] * long(pow(3, right_q[i] - 1)))) % M;
      ans = ans % M;
      //ans = pow(3, left_q[i]);
      //cout << ans << endl;
    }
  }

  cout << ans << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:95:170: error: invalid operands of types '__gnu_cxx::__promote<double>::__type' {aka 'double'} and 'll' {aka 'long long unsigned int'} to binary 'operator%'
   95 |       ans += ((left_a[i] * pow(3, left_q[i]) + left_q[i] * long(pow(3, left_q[i] - 1))) * (right_c[i] * pow(3, right_q[i]) + right_q[i] * long(pow(3, right_q[i] - 1)))) % M;
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
      |                                                                                         |                                                                                  |
      |                                                                                         __gnu_cxx::__promote<double>::__type {aka double}                                  ll {aka long long unsigned int}