public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  String S = sc . next ( ) ;
  StringBuffer sb = new StringBuffer ( S ) ;
  S = sb . reverse ( ) . toString ( ) ;
  String [ ] T = S . split ( "" ) ;
  long C = 0 ;
  long BC = 0 ;
  long ABC = 0 ;
  long AB = 1 ;
  long X = 1 ;
  for ( String i : T ) {
    if ( "?" . equals ( i ) ) {
      ABC = ( ABC * 3 + BC ) % 1000000007 ;
      BC = ( BC * 3 + C ) % 1000000007 ;
      C = ( C * 3 + X ) % 1000000007 ;
      X = ( X * 3 ) % 1000000007 ;
    }
    else if ( "C" . equals ( i ) ) {
      AB = 0 ;
      C += X ;
    }
    else if ( "B" . equals ( i ) ) {
      BC += C ;
    }
    else if ( "A" . equals ( i ) ) {
      ABC += BC ;
    }
  }
  System . out . println ( ABC % 1000000007 ) ;
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String S = sc . next ( ) ; StringBuffer sb = new StringBuffer ( S ) ; S = sb . reverse ( ) . toString ( ) ; String [ ] T = S . split ( " " ) ; long C = 0 ; long BC = 0 ; long ABC = 0 ; long AB = 1 ; long X = 1 ; for ( String i : T ) { if ( " ? " . equals ( i ) ) { ABC = ( ABC * 3 + BC ) % 1000000007 ; BC = ( BC * 3 + C ) % 1000000007 ; C = ( C * 3 + X ) % 1000000007 ; X = ( X * 3 ) % 1000000007 ; } else if ( " C " . equals ( i ) ) { AB = 0 ; C += X ; } else if ( " B " . equals ( i ) ) { BC += C ; } else if ( " A " . equals ( i ) ) { ABC += BC ; } } System . out . println ( ABC % 1000000007 ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (array_type element: (type_identifier) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (string_literal))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (enhanced_for_statement type: (type_identifier) name: (identifier) value: (identifier) body: (block (if_statement condition: (parenthesized_expression (method_invocation object: (string_literal) name: (identifier) arguments: (argument_list (identifier)))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier))) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier))) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier))) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (decimal_integer_literal))))) alternative: (if_statement condition: (parenthesized_expression (method_invocation object: (string_literal) name: (identifier) arguments: (argument_list (identifier)))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (identifier)))) alternative: (if_statement condition: (parenthesized_expression (method_invocation object: (string_literal) name: (identifier) arguments: (argument_list (identifier)))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier)))) alternative: (if_statement condition: (parenthesized_expression (method_invocation object: (string_literal) name: (identifier) arguments: (argument_list (identifier)))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier)))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))))