#include #include #include #include using namespace std; using i64 = int64_t; constexpr int mod = static_cast(powl(10, 9)) + 7; string to_bin(const i64 a) { if(a == 0) { return "0"; } string s = bitset<64>(a).to_string(); int p = static_cast(s.find('1')); return s.substr(p); } int cmp(int x, int y) { if(x < y) { return 0; } if(x == y) { return 1; } return 2; } int calc(int x, int y, int state) { int nstate = state; if(nstate == 1) { nstate = cmp(x, y); } return nstate; } i64 f(i64 x) { string s = to_bin(x); int n = static_cast(s.size()); // dp[xがyより未満/丁度/超過][yがNより未満/丁度/超過][ANDがXORより未満/丁度/超過][XORがORより未満/丁度/超過] := パターン数 vector>>> dp(3, vector>>(3, vector>(3, vector(3, 0)))); dp[1][1][1][1] = 1; for(int i=0; i>>> ndp(3, vector>>(3, vector>(3, vector(3, 0)))); for(int state0=0; state0<3; ++state0) { for(int state1=0; state1<3; ++state1) { for(int state2=0; state2<3; ++state2) { for(int state3=0; state3<3; ++state3) { if(!dp[state0][state1][state2][state3]) { continue; } for(int xi=0; xi<2; ++xi) { for(int yi=0; yi<2; ++yi) { int nstate0 = calc(xi, yi, state0), nstate1 = calc(yi, s[i]-'0', state1), nstate2 = calc(xi & yi, xi ^ yi, state2), nstate3 = calc(xi ^ yi, xi | yi, state3); ndp[nstate0][nstate1][nstate2][nstate3] += dp[state0][state1][state2][state3]; ndp[nstate0][nstate1][nstate2][nstate3] %= mod; } } } } } } dp = ndp; } i64 res = 0; for(int state0=0; state0<2; ++state0) { for(int state1=0; state1<2; ++state1) { res += dp[state0][state1][0][0]; res %= mod; } } return res; } int main(void) { i64 N; scanf("%ld", &N); i64 res = f(N); printf("%ld\n", res); return 0; }