#include <bits/stdc++.h>
using namespace std;
int val[5100], L[5100], R[5100], cnt[4], tot;
int f[5100][4];
int mex(int x, int y) {
  if (x != 0 && y != 0) return 0;
  if (x != 1 && y != 1) return 1;
  return 2;
}
int getRK(int x) {
  int l, r, mid;
  l = 0, r = 0x3f3f3f3f;
  while (l <= r) {
    mid = l + r >> 1;
    if (mid - mid / 3 <= x)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return r;
}
int main() {
  int n, m, i, j, k, temp, last, ans;
  scanf("%d%d", &n, &m);
  L[tot = 1] = 0, R[tot] = 2;
  m--;
  cnt[0] = m + m - 1;
  for (i = 3, j = 1, k = 1; i <= m; i = last + 1) {
    last = min(m, min(R[j] * 3 + 2, temp = getRK(R[k])));
    if (last < i) continue;
    tot++;
    L[tot] = i;
    R[tot] = last;
    val[tot] = mex(val[j], val[k]);
    cnt[val[tot]] = (cnt[val[tot]] + (long long)(m - i + 1 + m - last + 1) *
                                         (last - i + 1) / 2 % 1000000007) %
                    1000000007;
    if (val[tot] == val[tot - 1]) {
      R[tot - 1] = R[tot];
      tot--;
    }
    if (last == R[j] * 3 + 2) j++;
    if (last == temp) k++;
  }
  f[0][0] = 1;
  for (i = 1; i <= n; i++) {
    for (j = 0; j < 4; j++) {
      for (k = 0; k < 4; k++) {
        f[i][j] += (long long)f[i - 1][k] * cnt[j ^ k] % 1000000007;
        if (f[i][j] >= 1000000007) f[i][j] -= 1000000007;
      }
    }
  }
  ans = ((long long)f[n][1] + f[n][2] + f[n][3]) % 1000000007;
  printf("%d\n", ans);
  return 0;
}
