public static void main ( String [ ] args ) {
  Scanner scanner = new Scanner ( System . in ) ;
  char [ ] s = scanner . next ( ) . toCharArray ( ) ;
  int n = s . length ;
  long dp [ ] [ ] = new long [ n + 1 ] [ 4 ] ;
  for ( int i = 0 ;
  i <= 3 ;
  i ++ ) {
    dp [ n ] [ i ] = i == 3 ? 1 : 0 ;
  }
  for ( int i = n - 1 ;
  i >= 0 ;
  i -- ) {
    for ( int j = 3 ;
    j >= 0 ;
    j -- ) {
      dp [ i ] [ j ] = dp [ i + 1 ] [ j ] * ( s [ i ] == '?' ? 3L : 1L ) ;
      if ( j < 3 && ( s [ i ] == '?' || s [ i ] == "ABC" . toCharArray ( ) [ j ] ) ) {
        dp [ i ] [ j ] += dp [ i + 1 ] [ j + 1 ] ;
      }
      dp [ i ] [ j ] %= 1_000_000_007L ;
    }
  }
  System . out . println ( dp [ 0 ] [ 0 ] ) ;
}

public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; char [ ] s = scanner . next ( ) . toCharArray ( ) ; int n = s . length ; long dp [ ] [ ] = new long [ n + 1 ] [ 4 ] ; for ( int i = 0 ; i <= 3 ; i ++ ) { dp [ n ] [ i ] = i == 3 ? 1 : 0 ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 3 ; j >= 0 ; j -- ) { dp [ i ] [ j ] = dp [ i + 1 ] [ j ] * ( s [ i ] == ' ? ' ? 3L : 1L ) ; if ( j < 3 && ( s [ i ] == ' ? ' || s [ i ] == " ABC " . toCharArray ( ) [ j ] ) ) { dp [ i ] [ j ] += dp [ i + 1 ] [ j + 1 ] ; } dp [ i ] [ j ] %= 1_000_000_007L ; } } System . out . println ( dp [ 0 ] [ 0 ] ) ; }
(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: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal))) dimensions: (dimensions_expr (decimal_integer_literal))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (ternary_expression condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal)))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (parenthesized_expression (ternary_expression condition: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal)) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (parenthesized_expression (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal)) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (method_invocation object: (string_literal) name: (identifier) arguments: (argument_list)) index: (identifier))))))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (decimal_integer_literal)))))))