fun main(args: Array<String>) {
    val MOD = 1000000007L
    val dp = linkedMapOf<Pair<Long, Long>, Long>()
    fun f(S: Long, X: Long): Long {
        val d = dp[S to X]
        if (d != null) return d!!
        if ((S == 0L && X == 0L) || (S == 1L && X == 0L) || (S == 0L && X == 1L)) return 1
        var ans = f(S / 2, X / 2) % MOD
        if (S >= 1 && X >= 1) ans = (ans + f((S - 1) / 2, (X - 1) / 2)) % MOD
        if (S >= 2) ans = (ans + f((S - 2) / 2, X / 2)) % MOD
        dp[S to X] = ans
        return ans
    }
    val N = nextLong()
    val ans = f(N, N)
    println(ans)
}

fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun nextLong() = next().toLong()
