#include <bits/stdc++.h>
using namespace std;
const int maxn = 102, mod = 1000000007;
int l[] = {
    0,         1,         3,         4,         5,         7,         9,
    13,        15,        19,        27,        39,        40,        57,
    58,        81,        85,        120,       121,       174,       179,
    255,       260,       363,       382,       537,       544,       780,
    805,       1146,      1169,      1632,      1718,      2415,      2447,
    3507,      3622,      5154,      5260,      7341,      7730,      10866,
    11011,     15780,     16298,     23190,     23669,     33033,     34784,
    48894,     49549,     71007,     73340,     104352,    106510,    148647,
    156527,    220020,    222970,    319530,    330029,    469581,    479294,
    668910,    704371,    990087,    1003364,   1437882,   1485130,   2113113,
    2156822,   3010092,   3169669,   4455390,   4515137,   6470466,   6683084,
    9509007,   9705698,   13545411,  14263510,  20049252,  20318116,  29117094,
    30073877,  42790530,  43675640,  60954348,  64185794,  90221631,  91431521,
    131026920, 135332446, 192557382, 196540379, 274294563, 288836072, 405997338,
    411441844, 589621137, 608996006, 866508216, 884431705};
int r[] = {
    0,         2,         3,         4,         6,         8,         12,
    14,        18,        26,        38,        39,        56,        57,
    80,        84,        119,       120,       173,       178,       254,
    259,       362,       381,       536,       543,       779,       804,
    1145,      1168,      1631,      1717,      2414,      2446,      3506,
    3621,      5153,      5259,      7340,      7729,      10865,     11010,
    15779,     16297,     23189,     23668,     33032,     34783,     48893,
    49548,     71006,     73339,     104351,    106509,    148646,    156526,
    220019,    222969,    319529,    330028,    469580,    479293,    668909,
    704370,    990086,    1003363,   1437881,   1485129,   2113112,   2156821,
    3010091,   3169668,   4455389,   4515136,   6470465,   6683083,   9509006,
    9705697,   13545410,  14263509,  20049251,  20318115,  29117093,  30073876,
    42790529,  43675639,  60954347,  64185793,  90221630,  91431520,  131026919,
    135332445, 192557381, 196540378, 274294562, 288836071, 405997337, 411441843,
    589621136, 608996005, 866508215, 884431704, 1000000000};
int v[] = {0, 0, 1, 2, 1, 2, 0, 1, 2, 0, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0,
           2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2,
           1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1,
           2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2,
           0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1, 2, 0, 2, 1};
int n, p;
long long ans, cnt[3], fac[1001], rfac[1001];
long long quickpow(long long x, long long y) {
  long long ret = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) ret = ret * x % mod;
  return ret;
}
long long C(int x, int y) { return fac[x] * rfac[y] % mod * rfac[x - y] % mod; }
int main() {
  cin >> n >> p;
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod;
  for (int i = 0; i <= n; i++) rfac[i] = quickpow(fac[i], mod - 2);
  for (int i = 1; i <= maxn; i++)
    if (l[i] > p)
      break;
    else if (r[i] <= p) {
      long long t = 2 * p - l[i] - r[i];
      t *= (r[i] - l[i] + 1);
      t >>= 1;
      cnt[v[i]] += t % mod;
    } else {
      long long t = p - l[i];
      t *= p - l[i] + 1;
      t >>= 1;
      cnt[v[i]] += t % mod;
    }
  for (int i = 0; i < 3; i++) cnt[i] %= mod;
  for (int i = 0; i <= n; i += 2)
    for (int j = 0; j <= n - i; j += 2)
      ans += quickpow(cnt[0], n - i - j) * quickpow(cnt[1], i) % mod *
             quickpow(cnt[2], j) % mod * C(n, i) % mod * C(n - i, j) % mod;
  ans %= mod;
  ans = quickpow((long long)p * (p - 1) / 2 % mod, n) - ans;
  ans = (ans + mod) % mod;
  cout << ans << endl;
}
