object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextLong()

  val mod = 1000000007

  def plus(a: Long, b: Long): Long = (a + b) % mod
  def times(a: Long, b: Long): Long = (a * b) % mod

  val dp = Array.fill(61, 3)(0L)
  dp(60)(0) = 1L

  for (i <- 59 to 0 by -1) {
    if (((n >> i) & 1) == 0) {
      dp(i)(0) = plus(dp(i+1)(0), dp(i+1)(1))
      dp(i)(1) = dp(i+1)(1)
      dp(i)(2) = plus(dp(i+1)(1), times(3, dp(i+1)(2)))
    } else {
      dp(i)(0) = dp(i+1)(0)
      dp(i)(1) = plus(dp(i+1)(0), dp(i+1)(1))
      dp(i)(2) = plus(times(2, dp(i+1)(1)), times(3, dp(i+1)(2)))
    }
  }
  println(dp(0).reduceLeft(plus))
}