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;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
vector<pair<long long, long long> > adj[4];
int tmp[2][3];
void solve(vector<pair<long long, long long> >& v) {
  vector<pair<long long, long long> > now = v;
  v.clear();
  sort(now.begin(), now.end());
  long long st, en = -1;
  for (__typeof((now).begin()) it = (now).begin(); it != (now).end(); it++) {
    if (it->first <= en)
      umax(en, it->second);
    else {
      if (~en) v.push_back(make_pair(st, en));
      st = it->first;
      en = it->second;
    }
  }
  if (~en) v.push_back(make_pair(st, en));
}
map<long long, int> par;
struct matris {
  int m[3][3];
  void clear() { memset(m, 0, sizeof m); }
  void print() {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) cout << m[i][j] << " ";
      cout << endl;
    }
  }
} cur;
int mod(long long x) { return (x % 1000000007); }
matris mul(matris A, matris B) {
  matris C;
  C.clear();
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        C.m[i][j] = mod(C.m[i][j] + mod(A.m[i][k] * 1LL * B.m[k][j]));
  return C;
}
matris Fe(matris x, long long y) {
  if (y == 1) return x;
  matris h = Fe(x, y / 2);
  h = mul(h, h);
  if (y & 1) h = mul(h, x);
  return h;
}
int main() {
  int n;
  long long m;
  scanf("%d%lld", &n, &m);
  while (n--) {
    int x;
    long long l, r;
    scanf("%d%lld%lld", &x, &l, &r);
    adj[x].push_back(make_pair(l, r));
  }
  par[2] = par[m + 1] = 0;
  for (int i = 1; i <= 3; i++) {
    solve(adj[i]);
    for (__typeof((adj[i]).begin()) it = (adj[i]).begin(); it != (adj[i]).end();
         it++)
      par[it->first] ^= (1 << (i - 1)), par[it->second + 1] ^= (1 << (i - 1));
  }
  int now = 0;
  matris ans;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      if (i == j)
        ans.m[i][j] = 1;
      else
        ans.m[i][j] = 0;
    }
  for (__typeof((par).begin()) it = (par).begin(); it != (par).end(); it++) {
    for (int j = 0; j < 3; j++) tmp[0][j] = now >> j & 1;
    now ^= it->second;
    for (int j = 0; j < 3; j++) tmp[1][j] = now >> j & 1;
    __typeof((par).begin()) tt = it;
    tt++;
    if (tt == par.end()) break;
    cur.clear();
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (!tmp[0][i] and !tmp[1][j] and abs(i - j) < 2) cur.m[i][j] = 1;
    ans = mul(ans, cur);
    if (tt->first - it->first > 1) {
      cur.clear();
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
          if (!tmp[1][i] and !tmp[1][j] and abs(i - j) < 2) cur.m[i][j] = 1;
      ans = mul(ans, Fe(cur, tt->first - it->first - 1));
    }
  }
  printf("%d\n", ans.m[1][1]);
  return 0;
}
