You are running through a rectangular field. This field can be represented as a matrix with 3 rows and m columns. (i, j) denotes a cell belonging to i-th row and j-th column.

You start in (2, 1) and have to end your path in (2, m). From the cell (i, j) you may advance to:

  * (i - 1, j + 1) — only if i > 1, 
  * (i, j + 1), or 
  * (i + 1, j + 1) — only if i < 3. 



However, there are n obstacles blocking your path. k-th obstacle is denoted by three integers ak, lk and rk, and it forbids entering any cell (ak, j) such that lk ≤ j ≤ rk.

You have to calculate the number of different paths from (2, 1) to (2, m), and print it modulo 109 + 7.

Input

The first line contains two integers n and m (1 ≤ n ≤ 104, 3 ≤ m ≤ 1018) — the number of obstacles and the number of columns in the matrix, respectively.

Then n lines follow, each containing three integers ak, lk and rk (1 ≤ ak ≤ 3, 2 ≤ lk ≤ rk ≤ m - 1) denoting an obstacle blocking every cell (ak, j) such that lk ≤ j ≤ rk. Some cells may be blocked by multiple obstacles.

Output

Print the number of different paths from (2, 1) to (2, m), taken modulo 109 + 7. If it is impossible to get from (2, 1) to (2, m), then the number of paths is 0.

Example

Input

2 5
1 3 4
2 2 3


Output

2
#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
  if (0 <= second && second < n && 0 <= first && first < m) return true;
  return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
    ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long int mod = 1e9 + 7;
pair<long long int, long long int> arr[11111];
long long int vv[4], dp3[11111][4][4], dp2[111111][3][3];
int ck[4];
vector<pair<int, pair<long long int, int> > > v;
struct mat3 {
  long long int arr[3][3];
  void init() {
    memset(arr, 0, sizeof(arr));
    arr[0][0] = 1;
    arr[1][1] = 1;
    arr[2][2] = 1;
  }
  mat3() {
    memset(arr, 0, sizeof(arr));
    arr[0][0] = 1;
    arr[0][1] = 1;
    arr[0][2] = 0;
    arr[1][0] = 1;
    arr[1][1] = 1;
    arr[1][2] = 1;
    arr[2][0] = 0;
    arr[2][1] = 1;
    arr[2][2] = 1;
  }
};
mat3 mult3(mat3 a, mat3 b) {
  mat3 c;
  for (int e = 0; e < 3; e++)
    for (int p = 0; p < 3; p++) c.arr[e][p] = 0;
  for (int e = 0; e < 3; e++) {
    for (int p = 0; p < 3; p++) {
      for (int q = 0; q < 3; q++) {
        c.arr[e][p] = (c.arr[e][p] + a.arr[e][q] * b.arr[q][p]) % mod;
      }
    }
  }
  return c;
}
mat3 calc3(long long int num) {
  mat3 c;
  if (num == 0) {
    c.init();
    return c;
  }
  if (num == 1) return c;
  mat3 now = calc3(num / 2);
  if (num % 2 == 0)
    return mult3(now, now);
  else {
    return mult3(c, mult3(now, now));
  }
}
pair<long long int, pair<long long int, long long int> > go3(long long int a,
                                                             long long int b,
                                                             long long int c,
                                                             long long int cn) {
  mat3 nn = calc3(cn);
  pair<long long int, pair<long long int, long long int> > cc =
      make_pair(0, make_pair(0, 0));
  cc.first = (a * nn.arr[0][0] + b * nn.arr[0][1] + c * nn.arr[0][2]) % mod;
  cc.second.first =
      (a * nn.arr[1][0] + b * nn.arr[1][1] + c * nn.arr[1][2]) % mod;
  cc.second.second =
      (a * nn.arr[2][0] + b * nn.arr[2][1] + c * nn.arr[2][2]) % mod;
  return cc;
}
struct mat2 {
  long long int arr[2][2];
  void init() {
    memset(arr, 0, sizeof(arr));
    arr[0][0] = 1;
    arr[1][1] = 1;
  }
  mat2() {
    memset(arr, 0, sizeof(arr));
    arr[0][0] = 1;
    arr[0][1] = 1;
    arr[1][0] = 1;
    arr[1][1] = 1;
  }
};
mat2 mult2(mat2 a, mat2 b) {
  mat2 c;
  for (int e = 0; e < 2; e++)
    for (int p = 0; p < 2; p++) c.arr[e][p] = 0;
  for (int e = 0; e < 2; e++) {
    for (int p = 0; p < 2; p++) {
      for (int q = 0; q < 2; q++) {
        c.arr[e][p] = (c.arr[e][p] + a.arr[e][q] * b.arr[q][p]) % mod;
      }
    }
  }
  return c;
}
mat2 calc2(long long int num) {
  mat2 c;
  if (num == 0) {
    c.init();
    return c;
  }
  if (num == 1) return c;
  mat2 now = calc2(num / 2);
  if (num % 2 == 0)
    return mult2(now, now);
  else {
    return mult2(c, mult2(now, now));
  }
}
pair<long long int, long long int> go2(long long int a, long long int b,
                                       long long int cn) {
  mat2 nn = calc2(cn);
  pair<long long int, long long int> cc = make_pair(0, 0);
  cc.first = (a * nn.arr[0][0] + b * nn.arr[0][1]) % mod;
  cc.second = (a * nn.arr[1][0] + b * nn.arr[1][1]) % mod;
  return cc;
}
bool tmr(pair<int, pair<long long int, int> > a,
         pair<int, pair<long long int, int> > b) {
  if (a.second.first != b.second.first) return a.second.first < b.second.first;
  return a.second.second < b.second.second;
}
int main(void) {
  int n;
  long long int m;
  scanf("%d%lld", &n, &m);
  for (int e = 1; e <= 3; e++) dp3[0][e][e] = 1;
  for (int e = 0; e < n; e++) {
    long long int l, r;
    int a;
    scanf("%d%lld%lld", &a, &l, &r);
    v.push_back(make_pair((1 << a), make_pair(l, 0)));
    v.push_back(make_pair((1 << a), make_pair(r + 1, 1)));
  }
  vv[2] = 1;
  sort(v.begin(), v.end(), tmr);
  long long int ll = 1, lb = 0;
  for (int e = 0; e < (int)v.size(); e++) {
    int bit = v[e].first;
    long long int first = v[e].second.first;
    int ty = v[e].second.second;
    if (ty == 0) {
      if (bit & (1 << 1)) ck[1]++;
      if (bit & (1 << 2)) ck[2]++;
      if (bit & (1 << 3)) ck[3]++;
    } else {
      if (bit & (1 << 1)) ck[1]--;
      if (bit & (1 << 2)) ck[2]--;
      if (bit & (1 << 3)) ck[3]--;
    }
    if (e == (int)v.size() - 1 || v[e].second.first != v[e + 1].second.first) {
      int val = lb;
      if (val == 0) {
        pair<long long int, pair<long long int, long long int> > nn =
            go3(vv[1], vv[2], vv[3], first - ll - 1);
        vv[1] = nn.first;
        vv[2] = nn.second.first;
        vv[3] = nn.second.second;
      } else if (val == 8) {
        pair<long long int, long long int> nn =
            go2(vv[1], vv[2], first - ll - 1);
        vv[1] = nn.first;
        vv[2] = nn.second;
      } else if (val == 2) {
        pair<long long int, long long int> nn =
            go2(vv[2], vv[3], first - ll - 1);
        vv[2] = nn.first;
        vv[3] = nn.second;
      }
      long long int vv2[4];
      memset(vv2, 0, sizeof(vv2));
      if (ck[1] == 0) vv2[1] = (vv[1] + vv[2]) % mod;
      if (ck[2] == 0) vv2[2] = (vv[1] + vv[2] + vv[3]) % mod;
      if (ck[3] == 0) vv2[3] = (vv[2] + vv[3]) % mod;
      for (int e = 1; e <= 3; e++) vv[e] = vv2[e];
      lb = (ck[1] > 0 ? 2 : 0) + (ck[2] > 0 ? 4 : 0) + (ck[3] > 0 ? 8 : 0);
      ll = v[e].second.first;
    }
  }
  pair<long long int, pair<long long int, long long int> > nn =
      go3(vv[1], vv[2], vv[3], m - ll);
  printf("%lld", nn.second.first);
  return 0;
}
