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;
long long N = 3;
void add_self(long long& a, long long b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
}
long long mul(long long a, long long b) { return (a * b) % 1000000007; }
struct Mat {
  vector<vector<long long>> matrix;
  Mat() { matrix.resize(N, vector<long long>(N)); }
  Mat operator*(const Mat& b) const {
    Mat c = Mat();
    for (long long i = 0; i < N; i++) {
      for (long long j = 0; j < N; j++) {
        for (long long k = 0; k < N; k++) {
          add_self(c.matrix[i][j], mul(matrix[i][k], b.matrix[k][j]));
        }
      }
    }
    return c;
  }
};
Mat expo(Mat m, long long p) {
  Mat answer = Mat();
  for (long long i = 0; i < N; i++) {
    answer.matrix[i][i] = 1;
  }
  while (p) {
    if (p & 1) {
      answer = answer * m;
    }
    m = m * m;
    p /= 2;
  }
  return answer;
}
struct Mat1 {
  vector<vector<long long>> matrix;
  Mat1() { matrix.resize(N - 1, vector<long long>(N - 1)); }
  Mat1 operator*(const Mat1& b) const {
    Mat1 c = Mat1();
    for (long long i = 0; i < N - 1; i++) {
      for (long long j = 0; j < N - 1; j++) {
        for (long long k = 0; k < N - 1; k++) {
          add_self(c.matrix[i][j], mul(matrix[i][k], b.matrix[k][j]));
        }
      }
    }
    return c;
  }
};
Mat1 expo(Mat1 m, long long p) {
  Mat1 answer = Mat1();
  for (long long i = 0; i < N - 1; i++) {
    answer.matrix[i][i] = 1;
  }
  while (p) {
    if (p & 1) {
      answer = answer * m;
    }
    m = m * m;
    p /= 2;
  }
  return answer;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  Mat mat = Mat();
  mat.matrix = {{1, 1, 0}, {1, 1, 1}, {0, 1, 1}};
  Mat1 mat1 = Mat1();
  mat1.matrix = {{1, 1}, {1, 1}};
  vector<pair<long long, long long>> v;
  for (long long i = 0; i < n; i++) {
    long long a, l, r;
    cin >> a >> l >> r;
    v.emplace_back(l, -a);
    v.emplace_back(r, a);
  }
  sort((v).begin(), (v).end());
  n = v.size();
  long long curr[3] = {0, 1, 0};
  long long pos = 1, color = 0;
  long long col[3] = {0};
  for (long long ii = 0; ii < n; ii++) {
    long long dist = v[ii].first - pos;
    pos = v[ii].first;
    long long a = v[ii].second;
    if (color == 0) {
      Mat matr = expo(mat, dist);
      long long temp[3] = {0};
      for (long long i = 0; i < N; i++) {
        for (long long j = 0; j < N; j++) {
          add_self(temp[i], mul(curr[j], matr.matrix[i][j]));
        }
      }
      for (long long i = 0; i < N; i++) curr[i] = temp[i];
    } else if (color == 1) {
      Mat1 matr1 = expo(mat1, dist);
      long long temp[3] = {0};
      for (long long i = 0; i < N - 1; i++) {
        for (long long j = 0; j < N - 1; j++)
          add_self(temp[i + 1], mul(curr[j + 1], matr1.matrix[i][j]));
      }
      for (long long i = 0; i < N; i++) curr[i] = temp[i];
      curr[0] = 0;
    } else if (color == 2) {
      curr[1] = 0;
    } else if (color == 3) {
      curr[0] = curr[1] = 0;
    } else if (color == 4) {
      Mat1 matr1 = expo(mat1, dist);
      long long temp[3] = {0};
      for (long long i = 0; i < N - 1; i++) {
        for (long long j = 0; j < N - 1; j++)
          add_self(temp[i], mul(curr[j], matr1.matrix[i][j]));
      }
      for (long long i = 0; i < N; i++) curr[i] = temp[i];
      curr[2] = 0;
    } else if (color == 5) {
      curr[0] = curr[2] = 0;
    } else if (color == 6) {
      curr[1] = curr[2] = 0;
    } else {
      cout << 0;
      return 0;
    }
    if (a < 0) {
      long long tempa = a;
      a = abs(a);
      col[a - 1]++;
      color |= (1 << (a - 1));
      a = tempa;
    } else {
      col[a - 1]--;
      if ((col[a - 1] == 0) && (color & (1 << (a - 1)))) {
        color ^= (1 << (a - 1));
      }
    }
    for (long long i = 0; i < 3; i++)
      if (color & (1 << i)) curr[i] = 0;
  }
  Mat matr = expo(mat, m - pos);
  long long temp[3] = {0};
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      add_self(temp[i], mul(curr[j], matr.matrix[i][j]));
    }
  }
  for (long long i = 0; i < N; i++) curr[i] = temp[i];
  cout << curr[1];
}
