#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); } i64 f(i64 x) { string s = to_bin(x); int n = static_cast(s.size()); // dp[yがN未満?][xがy未満?][ANDがOR未満?][XORがOR未満?] := パターン数 vector>>> dp(2, vector>>(2, vector>(2, vector(2, 0)))); dp[0][0][0][0] = 1; for(int i=0; i>>> ndp(2, vector>>(2, vector>(2, vector(2, 0)))); for(int less0=0; less0<2; ++less0) { for(int less1=0; less1<2; ++less1) { for(int less2=0; less2<2; ++less2) { for(int less3=0; less3<2; ++less3) { if(!dp[less0][less1][less2][less3]) { continue; } for(int yi=0; yi<(less0 ? 2 : s[i]-'0'+1); ++yi) { for(int xi=0; xi<(less1 ? 2 : yi+1); ++xi) { int _and = xi & yi, _xor = xi ^ yi, __or = xi | yi; if(!less2 && _and > _xor) { continue; } if(!less3 && _xor > __or) { continue; } int nless0 = less0 || yi < s[i]-'0', nless1 = less1 || xi < yi, nless2 = less2 || _and < _xor, nless3 = less3 || _xor < __or; ndp[nless0][nless1][nless2][nless3] += dp[less0][less1][less2][less3]; ndp[nless0][nless1][nless2][nless3] %= mod; } } } } } } dp = ndp; } i64 res = 0; for(int less0=0; less0<2; ++less0) { for(int less1=0; less1<2; ++less1) { res += dp[less0][less1][1][1]; res %= mod; } } return res; } int main(void) { i64 x; scanf("%ld", &x); i64 res = f(x); printf("%ld\n", res); return 0; }