// depends on: -

/*
usage:

tools::square(5) => 25
*/

namespace tools {
  template <typename T> constexpr T square(const T x) {
    return x * x;
  }
}
// depends on: -

/*
a == tools:quo(a, b) * b + tools::mod(a, b)
0 <= tools::mod(a, b) < std::abs(b)
*/

#include <utility>

namespace tools {
  template <typename T> constexpr T quo(const T lhs, const T rhs) {
    return lhs > 0 ? lhs / rhs : rhs > 0 ? -((-lhs - 1 + rhs) / rhs) : (-lhs - 1 + -rhs) / -rhs;
  }
  template <typename T> constexpr T mod(const T lhs, const T rhs) {
    return lhs - quo(lhs, rhs) * rhs;
  }
}
// depends on: -

/*
usage:

static_assert(tools::is_prime(P), "P must be prime.");
*/

#include <cstdint>
#include <limits>

namespace tools {
  namespace detail {
    namespace is_prime {
      constexpr std::uint_fast32_t mid(const std::uint_fast32_t low, const std::uint_fast32_t high) {
        return (low + high) / 2;
      }

      // precondition: low*low <= n, high*high > n.
      constexpr std::uint_fast32_t ceilsqrt(const std::uint_fast32_t n, const std::uint_fast32_t low, const std::uint_fast32_t high) {
        return low + 1 >= high
          ? high
          : (mid(low, high) * mid(low, high) == n)
          ? mid(low, high)
          : (mid(low, high) * mid(low, high) <  n)
          ? ceilsqrt(n, mid(low, high), high)
          : ceilsqrt(n, low, mid(low, high));
      }

      // returns ceiling(sqrt(n))
      constexpr std::uint_fast32_t ceilsqrt(const std::uint_fast32_t n) {
        return n < 3
          ? n
          : ceilsqrt(n, 1, std::uint_fast32_t(1) << (std::numeric_limits<std::uint_fast32_t>::digits / 2));
      }

      // returns true if n is divisible by an odd integer in
      // [2 * low + 1, 2 * high + 1).
      constexpr bool find_factor(const std::uint_fast32_t n, const std::uint_fast32_t low, const std::uint_fast32_t high) {
        return low + 1 >= high
          ? (n % (2 * low + 1)) == 0
          : (find_factor(n, low, mid(low, high)) || find_factor(n, mid(low, high), high));
      }
    }
  }

  constexpr bool is_prime(const std::uint_fast32_t n) {
    return n > 1
      && (n == 2
        || (n % 2 == 1
          && (n == 3
            || !detail::is_prime::find_factor(n, 1, (detail::is_prime::ceilsqrt(n) + 1) / 2))));
  }
}
// depends on: is_prime.cpp, quo_and_mod.cpp, square.cpp

/*
usage:

constexpr const std::uint_fast32_t p = 3;
tools::int_mod<p>(5) + tools::int_mod<p>(4) // => 0
*/

#include <cstdint>
#include <stdexcept>

namespace tools {
  template <std::uint_fast32_t P> class int_mod {
    static_assert(is_prime(P), "P must be prime.");

  private:
    std::uint_fast32_t m_value;

    constexpr static std::uint_fast32_t add_impl(const std::uint_fast32_t lhs, const std::uint_fast32_t rhs) {
      return (lhs + rhs) % P;
    }

    constexpr static std::uint_fast32_t sub_impl(const std::uint_fast32_t lhs, const std::uint_fast32_t rhs) {
      return (lhs - rhs + P) % P;
    }

    constexpr static std::uint_fast32_t mul_impl(const std::uint_fast32_t lhs, const std::uint_fast32_t rhs) {
      return static_cast<std::uint_fast32_t>(static_cast<std::uint_fast64_t>(lhs) * static_cast<std::uint_fast64_t>(rhs) % static_cast<std::uint_fast64_t>(P));
    }

    constexpr int_mod<P> pow_impl(const std::uint_fast32_t exponent) const {
      return exponent == 0
        ? int_mod<P>(1)
        : exponent % 2 == 0
          ? square(this->pow_impl(exponent / 2))
          : this->pow_impl(exponent - 1) * *this;
    }

  public:
    int_mod() :
      m_value(0) {
    }

    int_mod(const std::int_fast32_t value) :
      m_value(static_cast<std::uint_fast32_t>(mod(value, static_cast<std::int_fast32_t>(P)))) {
    }

    int_mod(const int_mod<P>& other) :
      int_mod(other.m_value) {
    }

    constexpr operator std::uint_fast32_t() const {
      return m_value;
    }

    constexpr friend int_mod<P> operator+(const int_mod<P>& lhs, const int_mod<P>& rhs) {
      return int_mod<P>(int_mod<P>::add_impl(lhs.m_value, rhs.m_value));
    }

    int_mod<P>& operator+=(const int_mod<P>& other) {
      m_value = int_mod<P>::add_impl(m_value, other.m_value);
      return *this;
    }

    constexpr friend int_mod<P> operator-(const int_mod<P>& lhs, const int_mod<P>& rhs) {
      return int_mod<P>(int_mod<P>::sub_impl(lhs.m_value, rhs.m_value));
    }

    int_mod<P>& operator-=(const int_mod<P>& other) {
      m_value = int_mod<P>::sub_impl(m_value, other.m_value);
      return *this;
    }

    constexpr friend int_mod<P> operator*(const int_mod<P>& lhs, const int_mod<P>& rhs) {
      return int_mod<P>(int_mod<P>::mul_impl(lhs.m_value, rhs.m_value));
    }

    int_mod<P>& operator*=(const int_mod<P>& other) {
      m_value = int_mod<P>::mul_impl(m_value, other.m_value);
      return *this;
    }

    constexpr int_mod<P> inv() const {
      return m_value > 0
        ? this->pow_impl(P - 2)
        : (throw std::invalid_argument("cannot divide by zero"));
    }

    constexpr int_mod<P> pow(const std::int_fast32_t exponent) const {
      return exponent >= 0
        ? this->pow_impl(static_cast<std::uint_fast32_t>(exponent))
        : this->pow_impl(static_cast<std::uint_fast32_t>(-exponent)).inv();
    }

    constexpr friend int_mod<P> operator/(const int_mod<P>& lhs, const int_mod<P>& rhs) {
      return lhs * rhs.inv();
    }

    int_mod<P>& operator/=(const int_mod<P>& other) {
      m_value = int_mod<P>::mul_impl(m_value, other.inv().m_value);
      return *this;
    }
  };
}
// depends on: int_mod.cpp

/*
usage:

tools::fact_mod_cache<3> cache(5);
cache.fact(5); //=> 0
*/

#include <limits>
#include <cstdint>
#include <cstdlib>
#include <vector>

namespace tools {
  template <std::uint_fast32_t P> class fact_mod_cache {
  private:
    std::size_t m_max;
    std::vector<int_mod<P>> m_data;
    std::vector<int_mod<P>> m_inv_data;

  public:
    fact_mod_cache(const std::size_t max) :
      m_max(max),
      m_data(max + 1),
      m_inv_data(max + 1) {
      m_data[0] = int_mod<P>(1);
      for (std::size_t i = 1; i <= max; ++i) {
        m_data[i] = m_data[i - 1] * int_mod<P>(static_cast<std::int_fast32_t>(i));
      }
      m_inv_data[max] = m_data[max].inv();
      for (std::size_t i = max - 1; i != std::numeric_limits<std::size_t>::max(); --i) {
        m_inv_data[i] = m_inv_data[i + 1] * int_mod<P>(static_cast<std::int_fast32_t>(i + 1));
      }
    }

    constexpr int_mod<P> fact(const std::size_t i) const {
      return m_data[i];
    }

    constexpr int_mod<P> inv_fact(const std::size_t i) const {
      return m_inv_data[i];
    }
  };
}
#include <iostream>
#include <cstdint>
#include <limits>
#include <numeric>

constexpr const std::uint_fast32_t P = 1e9 + 7;
const tools::fact_mod_cache<P> cache(1e5);

int main() {
  std::string s;
  std::cin >> s;
  std::uint_fast32_t n = s.size();

  std::vector<std::uint_fast32_t> left_a_count(n, 0);
  std::vector<std::uint_fast32_t> right_c_count(n, 0);
  std::vector<std::uint_fast32_t> left_q_count(n, 0);
  std::vector<std::uint_fast32_t> right_q_count(n, 0);
  std::vector<std::uint_fast32_t> bq_positions;
  std::uint_fast32_t q_count = 0;
  for (std::uint_fast32_t i = 0; i < n; ++i) {
    switch (s[i]) {
    case 'A':
      ++left_a_count[i];
      break;
    case 'B':
      bq_positions.push_back(i);
      break;
    case 'C':
      ++right_c_count[i];
      break;
    case '?':
      ++left_q_count[i];
      ++right_q_count[i];
      bq_positions.push_back(i);
      ++q_count;
      break;
    }
  }

  for (std::uint_fast32_t i = 1; i < n; ++i) {
    left_a_count[i] += left_a_count[i - 1];
    left_q_count[i] += left_q_count[i - 1];
  }
  for (std::uint_fast32_t i = n - 1; i > 0; --i) {
    left_a_count[i] = left_a_count[i - 1];
    left_q_count[i] = left_q_count[i - 1];
  }
  left_a_count[0] = 0;
  left_q_count[0] = 0;
  for (std::uint_fast32_t i = n - 2; i != std::numeric_limits<std::uint_fast32_t>::max(); --i) {
    right_c_count[i] += right_c_count[i + 1];
    right_q_count[i] += right_q_count[i + 1];
  }
  for (std::uint_fast32_t i = 0; i < n - 1; ++i) {
    right_c_count[i] = right_c_count[i + 1];
    right_q_count[i] = right_q_count[i + 1];
  }
  right_c_count[n - 1] = 0;
  right_q_count[n - 1] = 0;

  std::vector<tools::int_mod<P>> pow3(q_count + 1);
  pow3[0] = tools::int_mod<P>(1);
  for (std::uint_fast32_t i = 1; i <= q_count; ++i) {
    pow3[i] = pow3[i - 1] * tools::int_mod<P>(3);
  }

  tools::int_mod<P> result(0);
  for (const std::uint_fast32_t& bq_position : bq_positions) {
    const std::uint_fast32_t lq = left_q_count[bq_position];
    const std::uint_fast32_t a = left_a_count[bq_position];
    const std::uint_fast32_t rq = right_q_count[bq_position];
    const std::uint_fast32_t c = right_c_count[bq_position];
    const tools::int_mod<P> left_count = pow3[lq] * tools::int_mod<P>(a) + (lq > 0 ? pow3[lq - 1] * tools::int_mod<P>(lq) : tools::int_mod<P>(0));
    const tools::int_mod<P> right_count = pow3[rq] * tools::int_mod<P>(c) + (rq > 0 ? pow3[rq - 1] * tools::int_mod<P>(rq) : tools::int_mod<P>(0));
    result += left_count * right_count;
  }
  std::cout << static_cast<std::uint_fast32_t>(result) << std::endl;
  return 0;
}

Measured memory_bytes: 6772