#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const long long inf = 1ll << 31;
set<long long> cs;
map<int, int> hs;
long long dp[1010][5], sm[5];
int n, p;
int ff(int x) { return (--hs.upper_bound(x))->second; }
void add(long long c, int v) {
  if (hs.rbegin()->second == v) return;
  hs[c] = v;
  for (int i = 0; i < 3; i++) cs.insert(c * 3 + i);
  for (int i = 0; i < 3; i++)
    if ((c - i) % 2 == 0) cs.insert((c - i) / 2 * 3 + i);
}
int main() {
  hs[1] = 0;
  for (int i = 3; i < 6; i++) cs.insert(3);
  while (1) {
    if (*cs.begin() >= inf) break;
    int u = *cs.begin();
    cs.erase(cs.begin());
    for (int j = 0;; j++)
      if (j != ff(u / 3) && j != ff(u - u / 3)) {
        add(u, j);
        break;
      }
  }
  scanf("%d%d", &n, &p);
  for (map<int, int>::iterator it = hs.begin(); it->first <= p; it++) {
    map<int, int>::iterator nit = it;
    ++nit;
    long long l = it->first, r = min(nit->first - 1, p);
    sm[it->second] = (sm[it->second] + (p + p - l - r) * (r - l + 1) / 2) % mod;
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 3; k++)
        dp[i + 1][j ^ k] = (dp[i + 1][j ^ k] + dp[i][j] * sm[k]) % mod;
  printf("%I64d\n", (dp[n][1] + dp[n][2] + dp[n][3]) % mod);
}
