JATC loves Banh-mi (a Vietnamese food). His affection for Banh-mi is so much that he always has it for breakfast. This morning, as usual, he buys a Banh-mi and decides to enjoy it in a special way.First, he splits the Banh-mi into $$$n$$$ parts, places them on a row and numbers them from $$$1$$$ through $$$n$$$. For each part $$$i$$$, he defines the deliciousness of the part as $$$x_i \in \{0, 1\}$$$. JATC's going to eat those parts one by one. At each step, he chooses arbitrary remaining part and eats it. Suppose that part is the $$$i$$$-th part then his enjoyment of the Banh-mi will increase by $$$x_i$$$ and the deliciousness of all the remaining parts will also increase by $$$x_i$$$. The initial enjoyment of JATC is equal to $$$0$$$.For example, suppose the deliciousness of $$$3$$$ parts are $$$[0, 1, 0]$$$. If JATC eats the second part then his enjoyment will become $$$1$$$ and the deliciousness of remaining parts will become $$$[1, \_, 1]$$$. Next, if he eats the first part then his enjoyment will become $$$2$$$ and the remaining parts will become $$$[\_, \_, 2]$$$. After eating the last part, JATC's enjoyment will become $$$4$$$.However, JATC doesn't want to eat all the parts but to save some for later. He gives you $$$q$$$ queries, each of them consisting of two integers $$$l_i$$$ and $$$r_i$$$. For each query, you have to let him know what is the maximum enjoyment he can get if he eats all the parts with indices in the range $$$[l_i, r_i]$$$ in some order.All the queries are independent of each other. Since the answer to the query could be very large, print it modulo $$$10^9+7$$$.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 100\,000$$$). The second line contains a string of $$$n$$$ characters, each character is either '0' or '1'. The $$$i$$$-th character defines the deliciousness of the $$$i$$$-th part. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the segment of the corresponding query.

Print $$$q$$$ lines, where $$$i$$$-th of them contains a single integer — the answer to the $$$i$$$-th query modulo $$$10^9 + 7$$$.

NoteIn the first example:   For query $$$1$$$: One of the best ways for JATC to eats those parts is in this order: $$$1$$$, $$$4$$$, $$$3$$$, $$$2$$$.  For query $$$2$$$: Both $$$3$$$, $$$4$$$ and $$$4$$$, $$$3$$$ ordering give the same answer. In the second example, any order of eating parts leads to the same answer.

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end

module ModCalc = struct
  let md = 1000000007L
  (* let md = 998244353L *)
  let validate v =
    (v + (labs(v) / md + 1L) * md) mod md
  let _add_mod md u v = u mod md + v mod md |> validate
  let _sub_mod md u v = u mod md - v mod md |> validate
  let _mul_mod md u v = u mod md * v mod md |> validate
  let rec pw u v =
    if v=0L then 1L
    else if v=1L then u mod md
    else let p = pw u (v/2L) in
      if v mod 2L = 0L then _mul_mod md p p
      else _mul_mod md (_mul_mod md p p) u
  let _inv md v = pw v (md-2L) (* md must be prime; otherwise use extended Euclidean algorithm *)
  let _div_mod md u v =
    _mul_mod md u (_inv md v)
  let (+%)  u v = _add_mod md u v
  let (-%)  u v = _sub_mod md u v
  let ( *%) u v = _mul_mod md u v
  let (/%)  u v = _div_mod md u v
  let rec pow x n = if n = 0L then 1L
    else if n mod 2L = 1L then x *% pow x (n-1L)
    else let w = pow x (n/2L) in w *% w
  let fact_simple n = let rec f0 n v = if n = 0L then v else f0 (n-1L) (n*%v) in f0 n 1L
  type fact = Fact of int64 array * int64 array
  let fact_make n =
    let m = i32 n in
    let fact,invf = Array.make m 0L,Array.make m 0L in
    fact.(0) <- 1L;
    for i=1 to m-$1 do fact.(i) <- fact.(i-$1) *% (i64 i); done;
    invf.(m-$1) <- pow fact.(m-$1) (md-2L);
    for i=m-$2 downto 0 do invf.(i) <- invf.(i+$1) *% (i64 i + 1L); done; Fact (fact,invf)
  let fact_get i (Fact (fact,_)) = fact.(i)
  (* let fact_inv_get i (Fact (_,invf)) = invf.(i) (* not tested yet *) *)
  let ncr n r (Fact (fact,invf)) = fact.(n) *% invf.(r) *% invf.(n-$r)
end open ModCalc

let tp i =
  let i = ref i in
  let x = ref 2L in
  let r = ref 1L in
  while !i > 0L do
    if !i mod 2L = 1L then (
      r := !r *% !x;
    );
    x := !x *% !x;
    i /= 2L;
  done; !r

let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    (* printf "%Ld %Ld\n" one zero; *)
    let sum_one = tp one - 1L in
    (* printf "%Ld\n" sum_one; *)
    let sum_zero = sum_one * (tp zero - 1L) in
    (* printf "%Ld\n" sum_zero; *)
    printf "%Ld\n" (sum_one +% sum_zero)
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end

let md = 1000000007L
let ( *$ ) p q = ((p mod md) * (q mod md)) mod md
let pow n k =
  let rec f a n = function
  | 0L -> a
  | 1L -> n*$a
  | k when k mod 2L = 0L -> f a (n*$n) (k/2L)
  | k -> f (a*$n) (n*$n) (k/2L)
  in f 1L n k

let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = pow 2L one - 1L in
    let sum_zero = sum_one * (pow 2L zero - 1L) in
    printf "%Ld\n" @@ (sum_one + sum_zero) mod md
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end
module ModCalc_test = struct
  let md = 1000000007L
  (* let md = 998244353L *)
  (* be careful of negative int *)
  let (+%) p q = ((p mod md) + (q mod md)) mod md
  let (-%) p q = ((p mod md) - (q mod md)) mod md
  let ( *% ) p q = ((p mod md) * (q mod md)) mod md
  let rec pow_mod n k = (* to be tested *)
    let rec f a n = function
    | 0L -> a mod md
    | 1L -> a *% n
    | k when k mod 2L = 0L -> f a (n *% n) (k/2L)
    | k -> f (a *% n) n (k-1L)
    in f 1L n k
  let inv v = pow_mod v (md-2L) (* md must be prime; otherwise use extended Euclidean algorithm *)
  let (/%) p q = p *% (inv q)
  let fact_simple n = let rec f0 n v = if n = 0L then v else f0 (n-1L) (n*%v) in f0 n 1L
  type fact = Fact of int64 array * int64 array
  let fact_make n =
    let m = i32 n in
    let fact,invf = Array.make m 0L,Array.make m 0L in
    fact.(0) <- 1L;
    for i=1 to m-$1 do fact.(i) <- fact.(i-$1) *% (i64 i); done;
    invf.(m-$1) <- pow_mod fact.(m-$1) (md-2L);
    for i=m-$2 downto 0 do invf.(i) <- invf.(i+$1) *% (i64 i + 1L); done; Fact (fact,invf)
  let fact_get i (Fact (fact,_)) = fact.(i)
  let ncr n r (Fact (fact,invf)) = fact.(n) *% invf.(r) *% invf.(n-$r)
end
open ModCalc_test
let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = pow_mod 2L one - 1L in
    let sum_zero = sum_one * (pow_mod 2L zero - 1L) in
    printf "%Ld\n" @@ sum_one +% sum_zero
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end

let md = 1000000007L
let ( +$ ) p q = ((p mod md) + (q mod md)) mod md
let ( *$ ) p q = ((p mod md) * (q mod md)) mod md
let pow n k =
  let rec f a n = function
  | 0L -> a
  | 1L -> n*$a
  | k when k mod 2L = 0L -> f a (n*$n) (k/2L)
  | k -> f (a*$n) (n*$n) (k/2L)
  in f 1L n k

let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = pow 2L one - 1L in
    let sum_zero = sum_one * (pow 2L zero - 1L) in
    printf "%Ld\n" @@ sum_one +$ sum_zero
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end
module ModCalc_test = struct
  let md = 1000000007L
  (* let md = 998244353L *)
  (* be careful of negative int *)
  let (+%) p q = ((p mod md) + (q mod md)) mod md
  let (-%) p q = ((p mod md) - (q mod md)) mod md
  let ( *% ) p q = ((p mod md) * (q mod md)) mod md
  let rec pow_mod n k = (* to be tested *)
    let rec f a n = function
    | 0L -> a mod md
    | 1L -> a *% n
    | k when k mod 2L = 0L -> f a (n *% n) (k/2L)
    | k -> f (a *% n) n (k-1L)
    in f 1L n k
  (* let inv v = pow_mod v (md-2L) (* md must be prime; otherwise use extended Euclidean algorithm *)
  let (/%) p q = p *% (inv q)
  let fact_simple n = let rec f0 n v = if n = 0L then v else f0 (n-1L) (n*%v) in f0 n 1L
  type fact = Fact of int64 array * int64 array
  let fact_make n =
    let m = i32 n in
    let fact,invf = Array.make m 0L,Array.make m 0L in
    fact.(0) <- 1L;
    for i=1 to m-$1 do fact.(i) <- fact.(i-$1) *% (i64 i); done;
    invf.(m-$1) <- pow_mod fact.(m-$1) (md-2L);
    for i=m-$2 downto 0 do invf.(i) <- invf.(i+$1) *% (i64 i + 1L); done; Fact (fact,invf)
  let fact_get i (Fact (fact,_)) = fact.(i)
  let ncr n r (Fact (fact,invf)) = fact.(n) *% invf.(r) *% invf.(n-$r) *)
end
open ModCalc_test
let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = pow_mod 2L one - 1L in
    let sum_zero = sum_one * (pow_mod 2L zero - 1L) in
    printf "%Ld\n" @@ sum_one +% sum_zero
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end

module ModCalc = struct
  let md = 1000000007L
  (* let md = 998244353L *)
  let validate v =
    (v + (labs(v) / md + 1L) * md) mod md
  let _add_mod md u v = u mod md + v mod md |> validate
  let _sub_mod md u v = u mod md - v mod md |> validate
  let _mul_mod md u v = u mod md * v mod md |> validate
  (* let rec pw u v =
    if v=0L then 1L
    else if v=1L then u mod md
    else let p = pw u (v/2L) in
      if v mod 2L = 0L then _mul_mod md p p
      else _mul_mod md (_mul_mod md p p) u *)
  let rec pow_mod n k = (* to be tested *)
    let rec f a n = function
    | 0L -> a mod md
    | 1L -> _mul_mod md a n
    | k when k mod 2L = 0L -> f a (_mul_mod md n n) (k/2L)
    | k -> f (_mul_mod md a n) n (k-1L)
    in f 1L n k
  let _inv md v = pow_mod v (md-2L) (* md must be prime; otherwise use extended Euclidean algorithm *)
  let _div_mod md u v =
    _mul_mod md u (_inv md v)
  let (+%)  u v = _add_mod md u v
  let (-%)  u v = _sub_mod md u v
  let ( *%) u v = _mul_mod md u v
  let (/%)  u v = _div_mod md u v
  let rec pow x n = if n = 0L then 1L
    else if n mod 2L = 1L then x *% pow x (n-1L)
    else let w = pow x (n/2L) in w *% w
  let fact_simple n = let rec f0 n v = if n = 0L then v else f0 (n-1L) (n*%v) in f0 n 1L
  type fact = Fact of int64 array * int64 array
  let fact_make n =
    let m = i32 n in
    let fact,invf = Array.make m 0L,Array.make m 0L in
    fact.(0) <- 1L;
    for i=1 to m-$1 do fact.(i) <- fact.(i-$1) *% (i64 i); done;
    invf.(m-$1) <- pow fact.(m-$1) (md-2L);
    for i=m-$2 downto 0 do invf.(i) <- invf.(i+$1) *% (i64 i + 1L); done; Fact (fact,invf)
  let fact_get i (Fact (fact,_)) = fact.(i)
  (* let fact_inv_get i (Fact (_,invf)) = invf.(i) (* not tested yet *) *)
  let ncr n r (Fact (fact,invf)) = fact.(n) *% invf.(r) *% invf.(n-$r)
end open ModCalc
let tp k = pow 2L k
let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = tp one - 1L in
    let sum_zero = sum_one * (tp zero - 1L) in
    printf "%Ld\n" (sum_one +% sum_zero)
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

module CumulSumArray = struct
  let make a =
    let ln = Array.length a
    in let a1 = Array.make (ln+$1) 0L in for i=1 to ln do a1.(i) <- a1.(i-$1) + a.(i-$1) done; a1
  let sum l r a = (* 0-origin,closed *)
    if l>r then 0L else (a.(r+$1) - a.(l))
  let sum_1orig l r a = sum (l-$1) (r-$1) a
end

let md = 1000000007L
let pow n k =
  let rec f a n = function
  | 0L -> a
  | 1L -> (n*a) mod md
  | k when k mod 2L = 0L -> f a (n*n) (k/2L) mod md
  | k -> f (a*n) (n*n) (k/2L) mod md
  in f 1L n k

let () =
  let n,q = get_2_i64 0 in
  let a =
    let s = scanf " %s"@@id in
    let a = make (String.length s) 0L in
    String.iteri (fun i c -> a.(i) <- i64 @@ (int_of_char c) -$ (int_of_char '0')) s;
    a in
  let ones = CumulSumArray.make a in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    let one = CumulSumArray.sum_1orig (i32 l) (i32 r) ones in
    let zero = r - l + 1L - one in
    let sum_one = pow 2L one - 1L in
    let sum_zero = sum_one * (pow 2L zero - 1L) in
    printf "%Ld\n" @@ (sum_one + sum_zero) mod md
  )


A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.

The first line contains a positive integer n (1 ≤ n ≤ 100), then follow n lines containing three integers each: the xi coordinate, the yi coordinate and the zi coordinate of the force vector, applied to the body ( - 100 ≤ xi, yi, zi ≤ 100).

Print the word "YES" if the body is in equilibrium, or the word "NO" if it is not.

let list_init n f =
    let rec read_vectors n vectors =
        if n > 0
        then
            let coordinates = f() in
            read_vectors (n - 1) (coordinates :: vectors)
        else
            List.rev vectors
    in read_vectors n []

let vectors n = list_init n (fun i -> Scanf.scanf "%d %d %d " (fun s0 s1 s2 -> [s0; s1; s2]))

let input () =
    let number_of_vectors = Scanf.scanf "%d " (fun n -> n) in
    vectors number_of_vectors

let add_vectors [x; y; z] [xs; ys; zs] = [x + xs; y + ys; z + zs]

let get_sum list = List.fold_left(fun a b -> add_vectors a b) [0; 0; 0] list

let solve input =
    if get_sum input = [0; 0; 0]
    then
        "YES"
    else
        "NO"

let print result = Printf.printf "%s\n" result

let () = print (solve (input()))

let sum_vec (x1,y1,z1) (x2,y2,z2) =
	(x1+x2, y1+y2, z1+z2)
;;

let read_vec _ = Scanf.scanf "%d %d %d\n" (fun x y z -> (x,y,z));;

let () =
	let n = read_int () in
	let forces = Array.init n read_vec in
	let f = Array.fold_left sum_vec (0,0,0) forces in
		print_string (if f = (0,0,0) then "YES" else "NO")
;;

let solve n =
  let rec loop n x y z =
    if n <= 0
    then
      if x == 0 && y == 0 && z == 0
      then "YES"
      else "NO"
    else Scanf.scanf "%d %d %d\n" (fun a b c -> loop (n-1) (x+a) (y+b) (z+c))
  in
  loop n 0 0 0

let _ =
  Printf.printf "%s\n" (Scanf.scanf "%d\n" solve)

(* Codeforces 69A *)

type coordinate = { x: int; y: int; z: int }

let zero = { x = 0; y = 0; z = 0 }

let make_coordinate x y z = { x = x; y = y; z = z }

let add a b =
  { x = a.x + b.x; y = a.y + b.y; z = a.z + b.z }

let rec solve accu = function
  | 0 -> if accu = zero then "YES" else "NO"
  | n ->
    let c =
      Scanf.scanf "%d %d %d\n" (fun x y z -> make_coordinate x y z) in
    solve (add accu c) (n-1)

let _ =
  let n = read_int() in
  print_endline (solve zero n)


A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing?

The first line of the input contains two integer numbers n and d (2 ≤ n ≤ 2000, 1 ≤ d ≤ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 ≤ bi ≤ 106).

Output the minimal number of moves needed to make the sequence increasing.

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)
let read_int64 _ = Scanf.bscanf Scanf.Scanning.stdib " %Ld" (fun x -> x)
let (|>) x f = f x

open Int64

let () =
  let n = read_int 0 in
  let d = read_int64 0 in
  let a = Array.init n read_int64 in
  let rec go i l c =
    if i >= n then
      c
    else
      let t = div (sub (add l d) a.(i)) d |> max 0L in
      go (i+1) (add a.(i) (mul d t)) (add c t)
  in
  go 1 a.(0) 0L |> Printf.printf "%Ld\n"

let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d" (fun x -> x)
let (|>) x f = f x

let () =
  let x = read_int 0 in
  let rec go x i =
    if x > 0 || (x land 1) <> 0 then
      go (x-i) (i+1)
    else
      i-1
  in
  go (abs x) 1 |> Printf.printf "%d\n"


Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.

The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.

On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.

NoteIn the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.In the second sample Kirito's strength is too small to defeat the only dragon and win.

let read () = Scanf.scanf "%d %d " (fun n m -> (n, m))

let input () =
    let (hero_power, total) = read () in
    let rec loop dragons i =
        if i < total
        then
            let dragon = read () in
            loop (dragon :: dragons) (i + 1)
        else
            (hero_power, dragons)
    in loop [] 0

let fight hero_power (dragon_power, reward) =
    if hero_power > dragon_power
    then
        `victory (hero_power + reward)
    else
        `defeat

let tuple_compare (power_1, reward_1) (power_2, reward_2) =
    Pervasives.compare power_1 power_2

let solve (hero_power, dragons) =
    let dragons = List.sort (tuple_compare) dragons in
    let rec loop power list =
        match list with
        | (dragon :: tail) ->
            (match fight power dragon with
            | `victory (upd_power) -> loop upd_power tail
            | `defeat -> "NO")
        | [] -> "YES"
    in loop hero_power dragons

let print result = Printf.printf "%s\n" result
let () = print (solve (input ()))

let main =
  let (s,n) = Scanf.scanf "%d %d" (fun s n -> (s,n)) in
  let rec read_all acc n =
    if n = 0 then acc
    else Scanf.scanf " %d %d" (fun x y -> read_all ((x,y)::acc) (n-1)) in
  let dragons = read_all [] n in
  let sorted = List.sort compare dragons in
  let rec walk s dragons =
    match dragons with
    | [] -> "YES"
    | ((x,y) :: t) ->
	if s > x then walk (s+y) t
        else "NO" in
  Printf.printf "%s\n" (walk s sorted)

let main =
  let (s,n) = Scanf.scanf "%d %d" (fun s n -> (s,n)) in
  let rec read_all acc n =
    if n = 0 then acc
    else Scanf.scanf " %d %d" (fun x y -> read_all ((x,y)::acc) (n-1)) in
  let dragons = read_all [] n in
  let pcompare (_,y1) (_,y2) = - (compare y1 y2) in
  let sorted = List.sort pcompare dragons in
  let rec walk s dragons =
    match dragons with
    | [] -> "YES"
    | ((x,y) :: t) ->
	if s > x then walk (s+y) t
        else "NO" in
  Printf.printf "%s\n" (walk s sorted)


The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered. Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.

The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x,  y,  r, where (x, y) are the coordinates of the stadium's center ( -  103 ≤ x,  y ≤ 103), and r (1 ≤ r  ≤ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.

Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.

let (|>) x f = f x
let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f " (fun x -> x)

let eps = 1e-6

type point = float*float
type circle = point*float
let getx ((x,_),_) = x
let gety ((_,y),_) = y
let getr ((_,_),r) = r
let mid (ax,ay) (bx,by) = (ax+.bx)*.0.5, (ay+.by)*.0.5
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let div (x,y) w = x/.w, y/.w
let ortho (x,y) = y, -.x
let diff (ax,ay) (bx,by) = bx-.ax, by-.ay
let len x = dot x x |> sqrt
let dist a b = diff a b |> len

let read_circle _ =
  let x = read_float 0 in
  let y = read_float 0 in
  let r = read_float 0 in
  (x,y),r

let eq cs u v = abs_float (getr cs.(u)-.getr cs.(v)) < eps

let circumcenter a b c : point =
  let p1x,p1y as p1 = mid a c
  and p2 = mid b c in
  let d1x,d1y as d1 = ortho (diff a c)
  and d2 = ortho (diff b c) in
  let ratio = det (diff p1 p2) d2 /. det d1 d2 in
  p1x+.ratio*.d1x, p1y+.ratio*.d1y

(* circle where dist(X,O1)/r1 = dist(X,O2)/r2 *)
let solve (p1,r1 as c1) (p2,r2 as c2) : circle =
  let ans1 = div (add (mul r2 p1) (mul r1 p2)) (r1+.r2)
  and ans2 = div (add (mul r2 p1) (mul (-.r1) p2)) (r2-.r1) in
  (div (add ans1 ans2) 2.0), dist ans1 ans2 /. 2.0

let intersect c0 (p1,r1) (p2,r2) : unit =
  let dcntr = dist p1 p2 in
  if r1+.r2 >= dcntr && abs_float (r1-.r2) <= dcntr then
    let proj1 = (r1*.r1-.r2*.r2+.dcntr*.dcntr)/.(2.0*.dcntr) in
    let h = sqrt (r1*.r1-.proj1*.proj1) in
    let p = add p1 (mul (proj1/.dcntr) (diff p1 p2)) in
    let dlt = mul (h/.dcntr) (ortho (diff p1 p2)) in
    let ans1 = add p dlt
    and ans2 = sub p dlt in
    if dist ans1 c0 < dist ans2 c0 then
      Printf.printf "%.5f %.5f\n" (fst ans1) (snd ans1)
    else
      Printf.printf "%.5f %.5f\n" (fst ans2) (snd ans2)

let () =
  let cs = Array.init 3 read_circle in
  if eq cs 0 1 && eq cs 1 2 then
    let x,y = circumcenter (fst cs.(0)) (fst cs.(1)) (fst cs.(2))
    in Printf.printf "%.5f %.5f\n" x y
  else (
    (* radii are not all the same *)
    for i = 0 to 1 do
      if eq cs i 2 then (
        let t = cs.(i lxor 1) in
        cs.(i lxor 1) <- cs.(2);
        cs.(2) <- t
      )
    done;
    (* thereafter cs = x,x,y or x,y,z *)
    intersect (fst cs.(0)) (solve cs.(0) cs.(2)) (solve cs.(1) cs.(2))
  )

let (|>) x f = f x
let read_int _ = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_float _ = Scanf.bscanf Scanf.Scanning.stdib " %f " (fun x -> x)

let eps = 1e-6

type point = float*float
type circle = point*float
let getx ((x,_),_) = x
let gety ((_,y),_) = y
let getr ((_,_),r) = r
let mid (ax,ay) (bx,by) = (ax+.bx)*.0.5, (ay+.by)*.0.5
let det (ax,ay) (bx,by) = ax*.by-.ay*.bx
let dot (ax,ay) (bx,by) = ax*.bx+.ay*.by
let add (ax,ay) (bx,by) = ax+.bx, ay+.by
let sub (ax,ay) (bx,by) = ax-.bx, ay-.by
let mul w (x,y) = x*.w, y*.w
let div (x,y) w = x/.w, y/.w
let ortho (x,y) = y, -.x
let diff (ax,ay) (bx,by) = bx-.ax, by-.ay
let len x = dot x x |> sqrt
let dist a b = diff a b |> len

let read_circle _ =
  let x = read_float 0 in
  let y = read_float 0 in
  let r = read_float 0 in
  (x,y),r

let eq cs u v = abs_float (getr cs.(u)-.getr cs.(v)) < eps

let circumcenter a b c : point =
  let p1x,p1y as p1 = mid a c
  and p2 = mid b c in
  let d1x,d1y as d1 = ortho (diff a c)
  and d2 = ortho (diff b c) in
  let ratio = det (diff p1 p2) d2 /. det d1 d2 in
  p1x+.ratio*.d1x, p1y+.ratio*.d1y

(* circle where dist(X,O1)/r1 = dist(X,O2)/r2 *)
let solve (p1,r1 as c1) (p2,r2 as c2) : circle =
  let ans1 = div (add (mul r2 p1) (mul r1 p2)) (r1+.r2)
  and ans2 = div (add (mul r2 p1) (mul (-.r1) p2)) (r2-.r1) in
  (div (add ans1 ans2) 2.0), dist ans1 ans2 /. 2.0

let intersect c0 (p1,r1) (p2,r2) : unit =
  let dcntr = dist p1 p2 in
  if r1+.r2 >= dcntr then
    let proj1 = (r1*.r1-.r2*.r2+.dcntr*.dcntr)/.(2.0*.dcntr) in
    let h = sqrt (r1*.r1-.proj1*.proj1) in
    let p = add p1 (mul (proj1/.dcntr) (diff p1 p2)) in
    let dlt = mul (h/.dcntr) (ortho (diff p1 p2)) in
    let ans1 = add p dlt
    and ans2 = sub p dlt in
    if dist ans1 c0 < dist ans2 c0 then
      Printf.printf "%.5f %.5f\n" (fst ans1) (snd ans1)
    else
      Printf.printf "%.5f %.5f\n" (fst ans2) (snd ans2)

let () =
  let cs = Array.init 3 read_circle in
  if eq cs 0 1 && eq cs 1 2 then
    let x,y = circumcenter (fst cs.(0)) (fst cs.(1)) (fst cs.(2))
    in Printf.printf "%.5f %.5f\n" x y
  else (
    (* radii are not all the same *)
    for i = 0 to 1 do
      if eq cs i 2 then (
        let t = cs.(i lxor 1) in
        cs.(i lxor 1) <- cs.(2);
        cs.(2) <- t
      )
    done;
    (* thereafter cs = x,x,y or x,y,z *)
    intersect (fst cs.(0)) (solve cs.(0) cs.(2)) (solve cs.(1) cs.(2))
  )


Filled with optimism, Hyunuk will host a conference about how great this new year will be!The conference will have $$$n$$$ lectures. Hyunuk has two candidate venues $$$a$$$ and $$$b$$$. For each of the $$$n$$$ lectures, the speaker specified two time intervals $$$[sa_i, ea_i]$$$ ($$$sa_i \le ea_i$$$) and $$$[sb_i, eb_i]$$$ ($$$sb_i \le eb_i$$$). If the conference is situated in venue $$$a$$$, the lecture will be held from $$$sa_i$$$ to $$$ea_i$$$, and if the conference is situated in venue $$$b$$$, the lecture will be held from $$$sb_i$$$ to $$$eb_i$$$. Hyunuk will choose one of these venues and all lectures will be held at that venue.Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval $$$[x, y]$$$ overlaps with a lecture held in interval $$$[u, v]$$$ if and only if $$$\max(x, u) \le \min(y, v)$$$.We say that a participant can attend a subset $$$s$$$ of the lectures if the lectures in $$$s$$$ do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue $$$a$$$ or venue $$$b$$$ to hold the conference.A subset of lectures $$$s$$$ is said to be venue-sensitive if, for one of the venues, the participant can attend $$$s$$$, but for the other venue, the participant cannot attend $$$s$$$.A venue-sensitive set is problematic for a participant who is interested in attending the lectures in $$$s$$$ because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 100\,000$$$), the number of lectures held in the conference. Each of the next $$$n$$$ lines contains four integers $$$sa_i$$$, $$$ea_i$$$, $$$sb_i$$$, $$$eb_i$$$ ($$$1 \le sa_i, ea_i, sb_i, eb_i \le 10^9$$$, $$$sa_i \le ea_i, sb_i \le eb_i$$$).

Print "YES" if Hyunuk will be happy. Print "NO" otherwise. You can print each letter in any case (upper or lower).

NoteIn second example, lecture set $$$\{1, 3\}$$$ is venue-sensitive. Because participant can't attend this lectures in venue $$$a$$$, but can attend in venue $$$b$$$.In first and third example, venue-sensitive set does not exist.

module Int = struct
  type t = int

  let compare (a:t)(b:t) = compare a b
end
module IntSet = Set.Make(Int)
module StringSet = Set.Make(String)

module IntSetWithHash : sig
  type  t

  val create : unit->t
  val add : int ->  t ->  unit
  val remove : int ->  t ->  unit

  val hash : t -> string
end = struct
  type t =
    { set : (int, unit) Hashtbl.t
    ; mutable hash : string
    }

  let hash t = t.hash

  let create () = { set = Hashtbl.create 100000 ; hash = "0123456789abcdef" }

  let xorhash cur_hash x =
    let new_hash =
    Digest.string (string_of_int x)
      in
      String.mapi (fun i ch ->
          Char.chr ((Char.code ch) lxor (Char.code new_hash.[i])))
        cur_hash

  let add x t =
    if Hashtbl.mem t.set x then () else (
      Hashtbl.add t.set x ();
      t.hash <- xorhash t.hash x;
  )

  let remove x t =
    if not (Hashtbl.mem t.set x ) then () else (
      Hashtbl.remove t.set x ;
      t.hash <- xorhash t.hash x
    )
end

(* let sexp_of_int = Core_kernel.sexp_of_int
 * let sexp_of_list = Core_kernel.sexp_of_list *)
type event =
  | Start of int
  | End of int
(* [@@deriving sexp_of] *)

let () =
  let n = read_int () in
  let events1 = Array.create (2*n) (0,Start 0) in
  let events2 = Array.create (2*n) (0,Start 0) in
  for i = 0 to n-1 do
    Scanf.scanf " %d %d %d %d" (fun s1 e1 s2 e2 ->
        events1.(2*i+0) <- (s1, Start i);
        events1.(2*i+1) <- (e1, End i);
        events2.(2*i+0) <- (s2, Start i);
        events2.(2*i+1) <- (e2, End i));
  done;
  Printf.eprintf "done\n%!";
  let process xs =
    Array.sort (fun (ts1, ev1) (ts2, ev2) ->
        (* need start to appear before end *)
        match compare ts1 ts2 with
        | 0 ->
          (match ev1, ev2 with
            | Start _, End _ -> -1
            | End _, Start _ -> 1
            | _ -> 0)
        | c -> c)
      xs;
    (* Printf.printf !"%{sexp: event list}\n"  evs; *)
    let maxs = ref StringSet.empty in
    let cur = IntSetWithHash.create () in
    let  should_be_false =
      Array.fold_left (fun (is_interesting) (_, ev) ->
          match ev with
          | Start x -> (IntSetWithHash.add x cur; true)
          | End x ->
            if is_interesting then (
              (* Printf.eprintf "> %d\n%!" (IntSet.cardinal cur); *)
              maxs := StringSet.add (IntSetWithHash.hash cur) !maxs;
            );
            (IntSetWithHash.remove x cur; false)
        ) ( false)
        xs
    in
    assert (false = should_be_false);
    (* IntSetSet.iter (fun s -> Printf.printf !"%{sexp: int list}" (s |> IntSet.to_seq |> List.of_seq)) !maxs; *)
    (* print_newline (); *)
    !maxs
  in
  let maxs1 = process events1 in
  Printf.eprintf "done1\n%!";
  let maxs2 = process events2 in
  Printf.eprintf "done2\n%!";
  print_endline (if StringSet.equal maxs1 maxs2 then "YES" else "NO")
;;


Alice and Bob received $$$n$$$ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies.Check if they can do that.Note that candies are not allowed to be cut in half.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of candies that Alice and Bob received. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the weights of the candies. The weight of each candy is either $$$1$$$ or $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, output on a separate line:    "YES", if all candies can be divided into two sets with the same weight;  "NO" otherwise.  You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

NoteIn the first test case, Alice and Bob can each take one candy, then both will have a total weight of $$$1$$$.In the second test case, any division will be unfair.In the third test case, both Alice and Bob can take two candies, one of weight $$$1$$$ and one of weight $$$2$$$.In the fourth test case, it is impossible to divide three identical candies between two people.In the fifth test case, any division will also be unfair.

(* #load "str.cma" *)
module Stdlib = Pervasives;;
(* let std_inp = Stdlib.open_in "input.txt";; *)
let std_inp = Stdlib.stdin;;
let std_outp = Stdlib.stdout;;

let identity x = x;;

let recogn (ones, twos) item =
  if item == 1 then (ones + 1, twos)
  else (ones, twos + 1);;

let rec solve (ones, twos) n =
if n > twos / 2 then false
else
  let ones_debt = (twos - n * 2) * 2 in
  (* let _ = Printf.printf "Debt: %d\n" ones_debt in *)
  if ones >= ones_debt && (ones - ones_debt) mod 2 == 0 then true
  else solve (ones, twos) (n + 1);;

let solution inp outp =
  let _ = input_line inp in
  let (ones, twos)=
    input_line inp
      |> Str.split @@ Str.regexp " "
      |> List.map int_of_string
      |> List.fold_left recogn (0, 0)
  in
  (* let _ = Printf.printf "Ones: %d; Twos: %d\n" ones twos in *)
  let result = solve (ones, twos) 0 in
  let _ =
    if result then Printf.printf "YES\n"
    else Printf.printf "NO\n"
  in
  ();;

let rec run_test = function
| (0, _, _) -> ()
| (t, inp, outp) ->
  let _ = solution inp outp in
  run_test (t - 1, inp, outp);;

let (_:unit) =
  let inp = std_inp and outp = std_outp in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let _ = run_test (tests_count, inp, outp) in
  ();;

(** https://codeforces.com/contest/1472/problem/B *)

let get_candies () =
    let tbl = Array.make 2 0 in
    let candy_list =
        read_line ()
        |> Str.split (Str.regexp " ")
        |> List.map int_of_string in
    List.iter
        (fun candy -> tbl.(candy-1) <- tbl.(candy-1) + 1)
        candy_list;
    tbl

let fair_share tbl_candies =
    if tbl_candies.(1) mod 2 = 0
    then tbl_candies.(0) mod 2 = 0
    else tbl_candies.(0) - 2 >= 0 && (tbl_candies.(0) - 2) mod 2 = 0

let solve_n nb_cases =
    let rec helper i =
        if i > 0
        then (
            let _ = read_line () in
            let tbl_candies = get_candies () in
            (if fair_share tbl_candies then "YES" else "NO")
            |> print_endline;
            helper (i-1)
        ) in
    helper nb_cases

let () =
    let nb_cases = read_int () in
    solve_n nb_cases

(* #load "str.cma" *)
module Stdlib = Pervasives;;
(* let std_inp = Stdlib.open_in "input.txt";; *)
let std_inp = Stdlib.stdin;;
let std_outp = Stdlib.stdout;;

let identity x = x;;

let recogn (ones, twos) item =
  if item == 1 then (ones + 1, twos)
  else (ones, twos + 1);;

let rec solve (ones, twos) n =
if n > twos then false
else
  let ones_debt = (twos - n) * 2 in
  (* let _ = Printf.printf "Debt: %d\n" ones_debt in *)
  if ones > ones_debt && (ones - ones_debt) mod 2 == 0 then true
  else solve (ones, twos) (n + 1);;

let solution inp outp =
  let _ = input_line inp in
  let (ones, twos)=
    input_line inp
      |> Str.split @@ Str.regexp " "
      |> List.map int_of_string
      |> List.fold_left recogn (0, 0)
  in
  (* let _ = Printf.printf "Ones: %d; Twos: %d\n" ones twos in *)
  let result = solve (ones, twos) 0 in
  let _ =
    if result then Printf.printf "YES\n"
    else Printf.printf "NO\n"
  in
  ();;

let rec run_test = function
| (0, _, _) -> ()
| (t, inp, outp) ->
  let _ = solution inp outp in
  run_test (t - 1, inp, outp);;

let (_:unit) =
  let inp = std_inp and outp = std_outp in
  let tests_count = let line = input_line inp in Scanf.sscanf line "%d" identity in
  let _ = run_test (tests_count, inp, outp) in
  ();;


Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

The first line contains three integers m, t, r (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.  The next line contains m space-separated numbers wi (1 ≤ i ≤ m, 1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print  - 1.

NoteAnya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.

(* 0 = rien; 1= fantome ; 2 = bougie finie de brulée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 600 downto 300 do
		if (tab.(i) = 1) || (tab.(i) = 3)
			then begin
				let compte = ref 0 in
				for j= i - dure + 1 to i do
					if tab.(j) > 1
						then incr compte

				done;
				let manque = ref (besoin - !compte) in
				for j = i - dure + 1 to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 601 0 in
	for i=1 to m do
		tab.(300 + Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

exception Not_possible
let () =
  Scanf.scanf "%d %d %d\n" (fun m t r ->
    (* array of ghost times *)
    let g = Array.init m (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    (* queue of candles *)
    let q = Queue.create () in
    (* number of candles *)
    let c = ref r in
    (* maximum timeslot at which a candle was lit *)
    let m = ref (g.(0) - 1) in
    (* light r candles before the first ghost *)
    for i = g.(0) - r to g.(0) - 1 do
      Queue.add i q
    done;
    try
      Array.iter (fun w ->
        (* remove stale candles *)
        while not (Queue.is_empty q) && Queue.peek q + t < w do
          ignore (Queue.take q)
        done;
        let l = Queue.length q in
        (* add more candles if necessary *)
        for i = w - r + l to w - 1 do
          if i <= !m then
            raise Not_possible
          else begin
            Queue.add i q;
            incr c;
            m := i
          end
        done
      ) g;
      Printf.printf "%d\n" !c
    with Not_possible -> print_endline "-1"
  )

(* 0 = rien; 1= fantome ; 2 = bougie finie de posée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 820 downto 400 do
		if tab.(i) mod 3 = 1
			then begin
				let compte = ref 0 in
					for j= i - dure + 1 to i do
						if tab.(j) > 1
							then incr compte
					done;
				let manque = ref (besoin - !compte) in
				for j = i - dure + 1 to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 821 0 in
	for i=1 to m do
		tab.(400 + Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

(* 0 = rien; 1= fantome ; 2 = bougie finie de posée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 620 downto 300 do
		if tab.(i) mod 3 = 1
			then begin
				let compte = ref 0 in
					for j= i - dure + 1 to i do
						if tab.(j) > 1
							then incr compte
					done;
				let manque = ref (besoin - !compte) in
				for j = i - dure + 1 to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 621 0 in
	for i=1 to m do
		tab.(300 + Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

(* 0 = rien; 1= fantome ; 2 = bougie finie de posée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 620 downto 300 do
		if tab.(i) mod 3 = 1
			then begin
				let compte = ref 0 in
					for j= i - dure + 1 to i do
						if tab.(j) > 1
							then incr compte
					done;
				let manque = ref (besoin - !compte) in
				for j = i - dure + 1 to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 621 0 in
	for i=1 to m do
		tab.(Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

(* 0 = rien; 1= fantome ; 2 = bougie finie de posée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 820 downto 400 do
		if (tab.(i) = 1) || (tab.(i) = 3)
			then begin
				let compte = ref 0 in
				for j= i - dure + 1 to i do
					if tab.(j) > 1
						then incr compte

				done;
				let manque = ref (besoin - !compte) in
				for j = i - dure to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 821 0 in
	for i=1 to m do
		tab.(400 + Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;
(*
final ();;
*)

let test l dure besoin =
	let tab = Array.make 821 0 in
		List.iter (fun i -> tab.(400 + i) <- 1) l;
		f tab dure besoin;;

(test [10] 8 3 = 3,
test [5;8] 10 1 = 1,
test [10] 1 3 = -1);;

(* 0 = rien; 1= fantome ; 2 = bougie finie de posée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 620 downto 300 do
		if tab.(i) mod 3 = 1
			then begin
				let compte = ref 0 in
					for j= i - dure + 1 to i do
						if tab.(j) > 1
							then incr compte
					done;
				let manque = ref (besoin - !compte) in
				for j = i - dure + 1 to i do
					if (!manque > 0) && (tab.(j) < 3)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 621 0 in
	for i=1 to m do
		tab.(Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

(* 0 = rien; 1= fantome ; 2 = bougie finie de brulée ; bougie et fantome *)

let f tab dure besoin =
	let bug = ref false in
	for i= 600 downto 300 do
		if (tab.(i) = 1) || (tab.(i) = 3)
			then begin
				let compte = ref 0 in
				for j= i - dure to i do
					if tab.(j) > 1
						then incr compte

				done;
				let manque = ref (besoin - !compte) in
				for j = i - dure to i do
					if (!manque > 0) && (tab.(j) < 2)
						then (decr manque; tab.(j) <- tab.(j) + 2);
				done;
				if !manque > 0 then bug := true;
			     end
	done;
	if !bug
		then -1
		else (let compte = ref 0 in
			for i = 0 to Array.length tab - 1 do
				if tab.(i) > 1 then incr compte;
			done; !compte);;
let final () =
	let (m,t,r) = Scanf.scanf "%d %d %d " (fun i j k -> (i,j,k)) in
	let tab = Array.make 601 0 in
	for i=1 to m do
		tab.(300 + Scanf.scanf "%d " (fun i -> i)) <- 1;
	done;
	Printf.printf "%d " (f tab t r);;

final ();;

exception Not_possible
let () =
  Scanf.scanf "%d %d %d\n" (fun m t r ->
    (* array of ghost times *)
    let g = Array.init m (fun _ -> Scanf.scanf "%d%_c" (fun x -> x)) in
    (* queue of candles *)
    let q = Queue.create () in
    (* number of candles *)
    let c = ref r in
    (* maximum timeslot at which a candle was lit *)
    let m = ref (g.(0) - 1) in
    (* light r candles before the first ghost *)
    for i = g.(0) - r - 1 to g.(0) - 1 do
      Queue.add i q
    done;
    try
      Array.iter (fun w ->
        (* remove stale candles *)
        while not (Queue.is_empty q) && Queue.peek q + t < w do
          ignore (Queue.take q)
        done;
        let l = Queue.length q in
        (* add more candles if necessary *)
        for i = w - r + l - 1 to w - 1 do
          if i <= !m then
            raise Not_possible
          else begin
            Queue.add i q;
            incr c;
            m := i
          end
        done
      ) g;
      Printf.printf "%d\n" !c
    with Not_possible -> print_endline "-1"
  )