#include <bits/stdc++.h>
using namespace std;
int n, m, a[100050][2], st, r1, r2;
long long ans, s[3], cnt;
struct Matrix {
  long long a[4][4];
};
Matrix operator*(const Matrix &x, const Matrix &y) {
  Matrix S;
  memset(S.a, 0, sizeof(S.a));
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        S.a[i][k] =
            (S.a[i][k] + x.a[i][j] * y.a[j][k] % 1000000007) % 1000000007;
  return S;
}
void Solve() {
  Matrix x, z;
  int y = n;
  memset(z.a, 0, sizeof(z.a));
  memset(x.a, 0, sizeof(x.a));
  for (int i = 0; i < 4; i++) z.a[i][i] = 1, x.a[i][i] = s[0];
  x.a[0][1] = x.a[1][0] = x.a[2][3] = x.a[3][2] = s[1];
  x.a[0][2] = x.a[2][0] = x.a[1][3] = x.a[3][1] = s[2];
  while (y) z = y & 1 ? z * x : z, x = x * x, y >>= 1;
  ans = (z.a[0][1] + z.a[0][2] + z.a[0][3]) % 1000000007;
}
inline int Calc(int x, int y) {
  return x > 0 && y > 0 ? 0 : (x != 1 && y != 1 ? 1 : 2);
}
inline int calc(int x) { return x & 1 ? (x / 2) * 3 + 1 : (x / 2) * 3 - 1; }
inline long long cAlc(long long x, long long y) {
  return (y - x + 1) * m - (y + x) * (y - x + 1) / 2;
}
int main() {
  cin >> n >> m;
  a[st = 1][1] = 2;
  r1 = 1;
  r2 = 1;
  for (int i = 3; i <= m; i++) {
    a[++st][0] = Calc(a[r1][0], a[r2][0]);
    int k = (a[r1][1] + 1) * 3, l = calc(a[r2][1] + 1);
    a[st][1] = i = min(k, l) - 1;
    if (a[st][0] == a[st - 1][0]) a[st - 1][1] = a[st][1], st--;
    r1 += r1 != st && k <= l;
    r2 += r2 != st && l <= k;
  }
  a[st][1] = min(a[st][1], m);
  for (int i = 1; i <= st; i++)
    s[a[i][0]] = (s[a[i][0]] + cAlc(a[i - 1][1] + 1, a[i][1])) % 1000000007;
  Solve();
  cout << ans << endl;
  return 0;
}
