#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void debug(...) {}
using lll = __int128;
const int maxn = 300010, p = 1e9 + 7;
ll p3[maxn];
ll solve(int bit, ll n) {
  if (bit == -1 || n == 0) return 1;
  static int cnt;
  if (((((lll)2ull << bit) - 1) << 1) <= n) {
    return p3[bit + 1];
  }
  ll res = 0;
  if (((lll)2ull << bit) <= n) res += solve(bit - 1, n - (2ull << bit));
  if (((lll)1ull << bit) <= n) res += solve(bit - 1, n - (1ull << bit));
  return res + solve(bit - 1, n);
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  ll n;
  cin >> n;
  p3[0] = 1;
  for (int i = 1; i < 100; ++i) p3[i] = p3[i - 1] * 3 % p;
  cout << solve(60, n) << '\n';
}
