public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  String S = sc . next ( ) ;
  long mod = 1000000007L ;
  String ABC = "ABC" ;
  long [ ] [ ] dp = new long [ S . length ( ) ] [ 4 ] ;
  if ( S . charAt ( 0 ) == '?' ) {
    dp [ 0 ] [ 0 ] = 3 ;
  }
  else {
    dp [ 0 ] [ 0 ] = 1 ;
  }
  if ( S . charAt ( 0 ) == 'A' || S . charAt ( 0 ) == '?' ) {
    dp [ 0 ] [ 1 ] = 1 ;
  }
  for ( int i = 1 ;
  i < S . length ( ) ;
  i ++ ) {
    for ( int j = 0 ;
    j < 4 ;
    j ++ ) {
      if ( j == 0 ) {
        if ( S . charAt ( i ) == 'A' || S . charAt ( i ) == 'B' || S . charAt ( i ) == 'C' ) {
          dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ;
        }
        else {
          dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] * 3L ;
        }
        dp [ i ] [ j ] %= mod ;
      }
      else {
        long x = ( S . charAt ( i ) == '?' ? 3L : 1L ) ;
        long y = ( ( S . charAt ( i ) == ABC . charAt ( j - 1 ) || S . charAt ( i ) == '?' ) ? 1L : 0L ) ;
        dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] * x ) + ( dp [ i - 1 ] [ j - 1 ] * y ) ;
        dp [ i ] [ j ] %= mod ;
      }
    }
  }
  System . out . println ( dp [ S . length ( ) - 1 ] [ 3 ] ) ;
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String S = sc . next ( ) ; long mod = 1000000007L ; String ABC = " ABC " ; long [ ] [ ] dp = new long [ S . length ( ) ] [ 4 ] ; if ( S . charAt ( 0 ) == ' ? ' ) { dp [ 0 ] [ 0 ] = 3 ; } else { dp [ 0 ] [ 0 ] = 1 ; } if ( S . charAt ( 0 ) == ' A ' || S . charAt ( 0 ) == ' ? ' ) { dp [ 0 ] [ 1 ] = 1 ; } for ( int i = 1 ; i < S . length ( ) ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) { if ( j == 0 ) { if ( S . charAt ( i ) == ' A ' || S . charAt ( i ) == ' B ' || S . charAt ( i ) == ' C ' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; } else { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] * 3L ; } dp [ i ] [ j ] %= mod ; } else { long x = ( S . charAt ( i ) == ' ? ' ? 3L : 1L ) ; long y = ( ( S . charAt ( i ) == ABC . charAt ( j - 1 ) || S . charAt ( i ) == ' ? ' ) ? 1L : 0L ) ; dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] * x ) + ( dp [ i - 1 ] [ j - 1 ] * y ) ; dp [ i ] [ j ] %= mod ; } } } System . out . println ( dp [ S . length ( ) - 1 ] [ 3 ] ) ; }
(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: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (string_literal))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) dimensions: (dimensions_expr (decimal_integer_literal))))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (character_literal))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (decimal_integer_literal)) right: (decimal_integer_literal)))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (decimal_integer_literal)) right: (decimal_integer_literal))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (character_literal)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (character_literal)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (decimal_integer_literal)) right: (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: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) 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 (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal))) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (decimal_integer_literal))))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (decimal_integer_literal)) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (decimal_integer_literal)) right: (decimal_integer_literal)))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier)))) alternative: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (parenthesized_expression (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (parenthesized_expression (ternary_expression condition: (parenthesized_expression (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal))))) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (binary_expression left: (parenthesized_expression (binary_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (identifier))) right: (parenthesized_expression (binary_expression left: (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))) right: (identifier)))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) index: (decimal_integer_literal)))))))