#include using namespace std; template class Array : public vector { public: using vector::vector; using vector::begin; using vector::end; using vector::push_back; template Array map(function f) { Array res(end() - begin()); for (auto it = begin(); it != end(); ++it) res[it - begin()] = f(*it); return res; } Array select(function f) { Array res; for (auto it = begin(); it != end(); ++it) if (f(*it)) res.push_back(*it); return res; } template R reduce(R u, function f) { for (auto it = begin(); it != end(); ++it) u = f(u, *it); return u; } template Array> zip(const Array &b) { assert(end() - begin() == b.end() - b.begin()); Array> res; for (auto it = begin(); it != end(); ++it) res.push_back({*it, b[it - begin()]}); return res; } Array iota(T st) { Array res(end() - begin()); ::iota(res.begin(), res.end(), st); return res; } Array slice(int st, size_t len) { auto it = (st < 0 ? end() : begin()) + st; return Array(it, it + len); } void inspect() { copy(begin(), prev(end()), ostream_iterator(cout, ", ")); cout << *prev(end()) << endl; } }; template struct ModInt { static int normal(int v) { return v < mod ? 0 <= v ? v : v + mod : v - mod; } int val; ModInt(int v = 0) : val(normal(v)) {} ModInt(int64_t v) : val(normal(v % mod)) {} ModInt operator+(const ModInt &b) const { return normal(val + b.val); } ModInt operator-(const ModInt &b) const { return normal(val - b.val); } ModInt operator*(const ModInt &b) const { return 1LL * val * b.val % mod; } ModInt operator/(const ModInt &oth) const { function modinv = [&](int a, int b, int x, int y) { if (b == 0) return x < 0 ? x + mod : x; return modinv(b, a - a / b * b, y, x - a / b * y); }; return *this * modinv(oth.val, mod, 1, 0); } ModInt operator-() const { return mod - val; } template friend ostream& operator<<(ostream& os, ModInt<_mod> m) { return os << m.val, os; } }; signed main() { ios::sync_with_stdio(false); int64_t N; cin >> N; string sn = bitset<64>(N).to_string(); Array asn = Array(find(sn.begin(), sn.end(), '1'), sn.end()) .template map([](char c) { return c - '0'; }); Array> dp((asn.size() + 1) * 2 * 2 * 2 * 2); dp[0] = 1; for (int i = 0; i < asn.size(); ++i) for (int lx = 0; lx < 2; ++lx) for (int ly = 0; ly < 2; ++ly) for (int l1 = 0; l1 < 2; ++l1) for (int l2 = 0; l2 < 2; ++l2) { int s = i << 4 | lx << 3 | ly << 2 | l1 << 1 | l2; for (int dx = 0; dx < (lx ? 2 : asn[i] + 1); ++dx) for (int dy = 0; dy < (ly ? 2 : dx + 1); ++dy) { if (!l1 & (dx & dy) > (dx ^ dy)) continue; if (!l2 & (dx ^ dy) > (dx | dy)) continue; int ns = i + 1 << 4 | (lx | dx < asn[i]) << 3 | (ly | dy < dx) << 2 | (l1 | (dx & dy) < (dx ^ dy)) << 1 | (l2 | (dx ^ dy) < (dx | dy)); dp[ns] = dp[ns] + dp[s]; } } ModInt ans; for (int lx = 0; lx < 2; ++lx) for (int ly = 0; ly < 2; ++ly) ans = ans + dp[asn.size() << 4 | lx << 3 | ly << 2 | 3]; cout << ans << endl; return 0; }