#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n;
map<long long, int> ms[70];
long long pw3[70];
long long ans = 0;
int main() {
  cin >> n;
  ms[60][n] = 1;
  pw3[0] = 1;
  for (int i = 1; i < 70; ++i) pw3[i] = (pw3[i - 1] * 3) % MOD;
  for (int i = 59; i >= 0; --i) {
    long long mx = (1ll << (i + 2)) - 2;
    for (pair<long long, int> e : ms[i + 1]) {
      long long x = e.first;
      long long c = e.second;
      if (x >= mx) {
        ans = (ans + c * pw3[i + 1]) % MOD;
        continue;
      }
      ms[i][x] += e.second;
      if (x >= (1ll << i)) ms[i][x - (1ll << i)] += e.second;
      if (x >= (1ll << (i + 1))) ms[i][x - (1ll << (i + 1))] += e.second;
    }
  }
  for (auto e : ms[0]) ans += e.second;
  ans %= MOD;
  cout << ans << "\n";
  return 0;
}
