123         +^ if 0 < count_q_lt.(i) the|~
124       (power 3 count_q_gt.(i) *^ cou|~
125         +^ if 0 < count_q_gt.(i) the|~
126               ;;                    |~
127 - : int array = [|0; 4; 4; 0|]      |~
128 #                                   |~
iexe-ocaml@9          128,2         末尾 abc.ml                                                                                                                                                                            33,20         末尾
:!cat abc.ml
let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

let rec power ( * ) e m n =
  if n <= 0 then e
  else if n mod 2 = 0 then
    power ( * ) e (m * m) (n / 2)
  else
    power ( * ) (e * m) m (n - 1)
let power = power ( *^ ) 1

let () =
  let s = read_line () in
  let count_a = Array.make (String.length s) 0 in
  let count_c = Array.make (String.length s) 0 in
  let count_q_lt = Array.make (String.length s) 0 in
  let count_q_gt = Array.make (String.length s) 0 in
  for i = 0 to String.length s - 2 do
    count_a.(i + 1) <- count_a.(i) + if s.[i] = 'A' then 1 else 0;
    count_q_lt.(i + 1) <- count_q_lt.(i) + if s.[i] = '?' then 1 else 0
  done;
  for i = String.length s - 1 downto 1 do
    count_c.(i - 1) <- count_c.(i) + if s.[i] = 'C' then 1 else 0;
    count_q_gt.(i - 1) <- count_q_gt.(i) + if s.[i] = '?' then 1 else 0
  done;
  Printf.printf "%d\n" @@ Array.fold_left ( +^ ) 0 @@ Array.init (String.length s) @@ fun i ->
    if s.[i] <> 'B' && s.[i] <> '?' then 0
    else
      (power 3 count_q_lt.(i) *^ count_a.(i)
        +^ count_q_lt.(i) *^ power 3 (count_q_lt.(i) - 1)) *^
      (power 3 count_q_gt.(i) *^ count_c.(i)
        +^ count_q_gt.(i) *^ power 3 (count_q_gt.(i) - 1))