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;
const int mod = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1 << 29;
int const NN = 1e3 + 3;
class Matrix {
 public:
  long long int a[100][100];
  long long int r;
  long long int c;
  Matrix();
  Matrix operator+(const Matrix &);
  Matrix operator*(const Matrix &);
  Matrix operator^(long long int);
  void display();
  void fill(long long int val);
};
Matrix::Matrix() { memset(a, 0, sizeof a); }
Matrix Matrix::operator+(const Matrix &m) {
  for (long long int i = 0; i < r; ++i) {
    for (long long int j = 0; j < c; ++j) a[i][j] = (a[i][j] + m.a[i][j]) % mod;
  }
  return *this;
}
void Matrix::display() {
  puts("-----");
  for (long long int i = 0; i < r; ++i) {
    for (long long int j = 0; j < c; ++j) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
  puts("-----");
}
void Matrix::fill(long long int val) {
  for (long long int i = 0; i < r; ++i) {
    for (long long int j = 0; j < c; ++j) a[i][j] = val;
  }
}
Matrix Matrix::operator*(const Matrix &m) {
  Matrix res;
  res.r = r;
  res.c = m.c;
  for (long long int i = 0; i < r; ++i) {
    for (long long int j = 0; j < m.c; ++j) {
      long long int sum = 0;
      for (int k = 0; k < c; ++k) {
        sum = (sum + a[i][k] * m.a[k][j]) % mod;
      }
      res.a[i][j] = sum;
    }
  }
  return res;
}
Matrix Matrix::operator^(long long int power) {
  assert(r == c);
  Matrix ret;
  ret.r = ret.c = r;
  for (long long int i = 0; i < r; ++i) ret.a[i][i] = 1;
  Matrix sq;
  sq.r = sq.c = r;
  sq = (*this);
  while (power) {
    if (power & 1) {
      ret = ret * sq;
    }
    power >>= 1;
    sq = sq * sq;
  }
  return ret;
}
pair<long long int, long long int> p[4][20004];
long long int totalP[4];
pair<pair<long long int, long long int>, long long int> q[20004];
long long int totalQ;
int main() {
  long long int n, m;
  scanf("%I64d", &n);
  scanf("%I64d", &m);
  for (long long int i = 1; i <= n; ++i) {
    long long int a, l, r;
    scanf("%I64d", &a);
    scanf("%I64d", &l);
    scanf("%I64d", &r);
    p[a][++totalP[a]] = make_pair(l, 1);
    p[a][++totalP[a]] = make_pair(r, 2);
  }
  for (long long int row = 1; row <= 3; ++row) {
    sort(p[row] + 1, p[row] + totalP[row] + 1);
  }
  for (long long int row = 1; row <= 3; ++row) {
    long long int start = -1;
    long long int balance = 0;
    for (long long int j = 1; j <= totalP[row]; ++j) {
      if (balance == 0) {
        ++balance;
        start = p[row][j].first;
        continue;
      }
      balance += p[row][j].second == 1;
      balance -= p[row][j].second == 2;
      if (balance == 0) {
        q[++totalQ] = make_pair(make_pair(start, p[row][j].first), row);
      }
    }
  }
  map<long long int, bool> isStart1, isEnd1, isStart2, isEnd2, isStart3, isEnd3;
  vector<long long int> cols;
  for (long long int i = 1; i <= totalQ; ++i) {
    if (q[i].second == 1) {
      isStart1[q[i].first.first] = true;
      isEnd1[q[i].first.second] = true;
    } else if (q[i].second == 2) {
      isStart2[q[i].first.first] = true;
      isEnd2[q[i].first.second] = true;
    } else {
      isStart3[q[i].first.first] = true;
      isEnd3[q[i].first.second] = true;
    }
    cols.push_back(q[i].first.first);
    cols.push_back(q[i].first.second);
  }
  sort(cols.begin(), cols.end());
  cols.erase(unique(cols.begin(), cols.end()), cols.end());
  Matrix DP;
  DP.r = 3;
  DP.c = 1;
  DP.a[1][0] = 1;
  Matrix C;
  C.r = C.c = 3;
  C.fill(1);
  C.a[0][2] = 0;
  C.a[2][0] = 0;
  long long int curCol = 2;
  long long int nextCol = cols.empty() ? m : cols[0] - 1ll;
  DP = (C ^ (nextCol - curCol + 1ll)) * DP;
  long long int colSz = cols.size();
  for (long long int i = 0; i < colSz; ++i) {
    if (isStart1[cols[i]]) {
      C.a[0][0] = C.a[0][1] = C.a[0][2] = 0;
    }
    if (isStart2[cols[i]]) {
      C.a[1][0] = C.a[1][1] = C.a[1][2] = 0;
    }
    if (isStart3[cols[i]]) {
      C.a[2][0] = C.a[2][1] = C.a[2][2] = 0;
    }
    DP = C * DP;
    if (isEnd1[cols[i]]) {
      C.a[0][0] = C.a[0][1] = 1;
      C.a[0][2] = 0;
    }
    if (isEnd2[cols[i]]) {
      C.a[1][0] = C.a[1][1] = C.a[1][2] = 1;
    }
    if (isEnd3[cols[i]]) {
      C.a[2][0] = 0;
      C.a[2][1] = C.a[2][2] = 1;
    }
    DP = (C ^ (((i == colSz - 1ll) ? m : cols[i + 1] - 1ll) - cols[i])) * DP;
  }
  cout << DP.a[1][0];
  putchar('\n');
  return 0;
}
