let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

let () = Scanf.scanf "%d" @@ fun n ->
  let dp = Array.make_matrix 63 3 0 in
  dp.(62).(2) <- 1;
  for i = 61 downto 0 do
    if n land (1 lsl i) <> 0 then begin
      dp.(i).(0) <- dp.(i + 1).(0) *^ 3 +^ 2 *^ dp.(i + 1).(1);
      dp.(i).(1) <- dp.(i + 1).(1) +^ dp.(i + 1).(2);
      dp.(i).(2) <- dp.(i + 1).(2)
    end else begin
      dp.(i).(0) <- dp.(i + 1).(0) *^ 3 +^ dp.(i + 1).(1);
      dp.(i).(1) <- dp.(i + 1).(1);
      dp.(i).(2) <- dp.(i + 1).(1) +^ dp.(i + 1).(2)
    end
  done;
  Printf.printf "%d\n" @@ Array.fold_left ( +^ ) 0 dp.(0)