#include <bits/stdc++.h>
using namespace std;
const int N = 1009, Mod = 1e9 + 7;
struct Data {
  int l, r, g;
};
vector<Data> A;
int n, p, G[3], C[N][N];
inline int Power(int a, int b) {
  int ret = 1;
  for (; b; b >>= 1, a = 1LL * a * a % Mod)
    if (b & 1) ret = 1LL * ret * a % Mod;
  return (ret);
}
int main() {
  cin >> n >> p;
  if (p <= 2) return !printf("0\n");
  A.push_back({1, 2, 0});
  int p1 = 0, p2 = 0;
  while (A.back().r < p) {
    int l = A.back().r + 1;
    while (A[p1].r < l / 3) p1++;
    while (A[p2].r < l - l / 3) p2++;
    int nxt1 = min(A[p1].r * 3LL + 2, (long long)2e9);
    int nxt2 = min((A[p2].r * 3LL) / 2, (long long)2e9);
    int r = min(nxt1, nxt2), g = -1;
    if (A[p1].g != 0 && A[p2].g != 0)
      g = 0;
    else if (A[p1].g != 1 && A[p2].g != 1)
      g = 1;
    else
      g = 2;
    if (A.back().g == g)
      A.back().r = r;
    else
      A.push_back({l, r, g});
  }
  A.back().r = p;
  for (Data d : A) {
    G[d.g] = (G[d.g] + 1LL * p * (d.r - d.l + 1)) % Mod;
    G[d.g] =
        (G[d.g] - 1LL * (d.r + d.l) * (d.r - d.l + 1) / 2 % Mod + Mod) % Mod;
  }
  for (int i = 0; i < N; i++)
    for (int j = C[0][i] = C[i][i] = 1; j < i; j++)
      C[j][i] = (C[j - 1][i - 1] + C[j][i - 1]) % Mod;
  int tot = 0;
  for (int i = 0; i <= n; i++)
    for (int j = 0; i + j <= n; j++)
      if (i % 2 == 1 || j % 2 == 1)
        tot = (tot + 1LL * C[i][n] * C[j][n - i] % Mod * Power(G[1], i) % Mod *
                         Power(G[2], j) % Mod * Power(G[0], n - i - j)) %
              Mod;
  return !printf("%d\n", tot);
}
