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 maxn = 1e5 + 10;
vector<pair<long long, long long> > v[3];
vector<pair<long long, int> > segment[3];
struct matrix {
  int arr[3][3];
  matrix(array<int, 3> a1, array<int, 3> a2) {
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        if (!a1[i] and !a2[j] and abs(i - j) < 2) {
          arr[i][j] = 1;
        } else
          arr[i][j] = 0;
      }
    }
  }
  matrix() {
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        if (i == j)
          arr[i][j] = 1;
        else
          arr[i][j] = 0;
      }
    }
  }
};
matrix mul(matrix a, matrix b) {
  matrix c;
  for (int i = 0; i <= 2; i++) {
    for (int j = 0; j <= 2; j++) {
      c.arr[i][j] = 0;
      for (int k = 0; k <= 2; k++) {
        c.arr[i][j] = (0ll + c.arr[i][j] +
                       (1ll * a.arr[i][k] * b.arr[k][j]) % 1000000007) %
                      1000000007;
      }
    }
  }
  return c;
}
matrix matexpo(matrix a, long long int steps) {
  if (!steps) {
    return matrix();
  }
  matrix temp = matexpo(a, steps / 2);
  temp = mul(temp, temp);
  if (steps % 2) {
    temp = mul(temp, a);
  }
  return temp;
}
vector<int> get(vector<int> old_ans, array<int, 3> oldcur, array<int, 3> newcur,
                long long int steps) {
  matrix alpha(oldcur, newcur);
  matrix temp = matexpo(alpha, steps);
  vector<int> loop_ans(3);
  for (int i = 0; i <= 2; i++) {
    for (int j = 0; j <= 2; j++) {
      loop_ans[j] = (0ll + loop_ans[j] +
                     (1ll * temp.arr[i][j] * old_ans[i]) % 1000000007) %
                    1000000007;
    }
  }
  return loop_ans;
}
void solve() {
  long long int n, m;
  scanf("%lld", &n);
  scanf("%lld", &m);
  vector<int> ans = {0, 1, 0};
  array<int, 3> cur = {0, 0, 0};
  for (int i = 1; i <= n; i++) {
    long long int a, l, r;
    scanf("%lld", &a);
    scanf("%lld", &l);
    scanf("%lld", &r);
    v[a - 1].push_back({l, r});
  }
  for (int i = 0; i <= 2; i++) {
    sort((v[i]).begin(), (v[i]).end());
    stack<pair<long long, long long> > s1;
    for (int j = 0; j <= (int)v[i].size() - 1; j++) {
      if (s1.empty() or s1.top().second < (v[i][j].first - 1)) {
        s1.push(v[i][j]);
      } else {
        s1.top().second = max(v[i][j].second, s1.top().second);
      }
    }
    v[i].clear();
    v[i].reserve(s1.size());
    while (!s1.empty()) {
      v[i].push_back(s1.top());
      s1.pop();
    }
    reverse((v[i]).begin(), (v[i]).end());
  }
  for (int i = 0; i <= 2; i++) {
    for (int j = 0; j <= (int)v[i].size() - 1; j++) {
      segment[i].push_back({v[i][j].first, 1});
      segment[i].push_back({v[i][j].second + 1, 0});
    }
    segment[i].push_back({m + 1, 0});
  }
  int i = 0, j = 0, k = 0;
  long long int pos = 1;
  while (true) {
    long long int next =
        min({segment[0][i].first, segment[1][j].first, segment[2][k].first});
    long long int steps = next - pos - 1;
    ans = get(ans, cur, cur, steps);
    if (next == (m + 1)) {
      printf("%d\n", ans[1]);
      return;
    }
    pos = next - 1;
    array<int, 3> newcur = cur;
    if (next == segment[0][i].first) {
      newcur[0] = segment[0][i].second;
      i++;
    }
    if (next == segment[1][j].first) {
      newcur[1] = segment[1][j].second;
      j++;
    }
    if (next == segment[2][k].first) {
      newcur[2] = segment[2][k].second;
      k++;
    }
    ans = get(ans, cur, newcur, 1);
    cur = newcur;
    pos++;
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
