#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
template <class T>
inline T isqrt(T k) {
  T r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
template <class T>
inline T icbrt(T k) {
  T r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
struct CentroidTree {
  struct Info {
    long long d;
    Info() : d(0) {}
    Info(long long d) : d(d) {}
    Info(const Info& rhs) : d(rhs.d) {}
    Info operator+(const int& w) const { return Info(d + w); }
    Info operator+(const Info& rhs) const { return Info(d + rhs.d); }
  };
  static const int maxn = 400000 + 10;
  static const int logn = 20;
  int n, rt;
  vector<pair<int, int> > adj[maxn];
  int size[maxn];
  int rem[maxn];
  vector<int> tree[maxn];
  int lev[maxn];
  int tin[maxn];
  int tou[maxn];
  int tms;
  int p[logn][maxn];
  Info info[logn][maxn];
  void init(int n) {
    this->n = n;
    for (int i = 0; i < n; i++) {
      adj[i].clear();
      tree[i].clear();
      rem[i] = 0;
    }
    tms = 0;
  }
  void add(int u, int v, int w) {
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  void dfs(int u, int p) {
    size[u] = 1;
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (v != p && !rem[v]) {
        dfs(v, u);
        size[u] += size[v];
      }
    }
  }
  int findcentroid(int u) {
    dfs(u, -1);
    int p = -1, t = size[u];
    while (1) {
      pair<int, int> best;
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        if (v != p && !rem[v]) {
          best = max(best, make_pair(size[v], v));
        }
      }
      if (best.first + best.first <= t) {
        return u;
      }
      p = u, u = best.second;
    }
  }
  void upd(int u, int dep, int p) {
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      int w = adj[u][i].second;
      if (v != p && !rem[v]) {
        info[dep][v] = info[dep][u] + w;
        upd(v, dep, u);
      }
    }
  }
  int divide(int u, int dep) {
    lev[u = findcentroid(u)] = dep;
    tin[u] = tms++;
    p[0][u] = u;
    info[dep][u] = Info(0);
    upd(u, dep, -1);
    rem[u] = 1;
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (!rem[v]) {
        int w = divide(v, dep + 1);
        tree[u].push_back(w);
        p[0][w] = u;
      }
    }
    tou[u] = tms - 1;
    adj[u].clear();
    return u;
  }
  int lca(int u, int v) {
    if (lev[u] < lev[v]) swap(u, v);
    for (int k = logn - 1; k >= 0; k--) {
      if (lev[p[k][u]] >= lev[v]) {
        u = p[k][u];
      }
    }
    if (u == v) return u;
    for (int k = logn - 1; k >= 0; k--) {
      if (p[k][u] != p[k][v]) {
        u = p[k][u], v = p[k][v];
      }
    }
    return p[0][u];
  }
  int isparent(int u, int v) { return tin[u] <= tin[v] && tou[u] >= tou[v]; }
  Info getInfo(int u, int v) {
    int a = lca(u, v);
    int ix = lev[a];
    return info[ix][u] + info[ix][v];
  }
  void build() {
    rt = divide(0, 0);
    for (int k = 1; k < logn; k++) {
      for (int u = 0; u < n; u++) {
        p[k][u] = p[k - 1][p[k - 1][u]];
      }
    }
    for (int u = 0; u < n; u++) {
      adj[u].clear();
    }
  }
} ct;
const int maxn = 400000 + 10;
int n, q;
int p[maxn];
vector<pair<int, int> > adj[maxn];
vector<pair<int, int> > adj2[maxn];
int vis[maxn];
void dfs(int u) {
  vis[u] = 1;
  vector<pair<int, int> > li;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    if (!vis[v]) {
      li.push_back(adj[u][i]);
    }
  }
  if (int((li).size()) <= 2) {
    adj2[u] = li;
  } else {
    adj2[u].push_back(li[0]);
    adj2[u].push_back(make_pair(n, 0));
    for (int i = (1); i < (int((li).size()) - 1); i++) {
      adj2[n].push_back(li[i]);
      if (i < int((li).size()) - 2) {
        adj2[n].push_back(make_pair(n + 1, 0));
      } else {
        adj2[n].push_back(li[i + 1]);
      }
      n++;
    }
  }
  for (int i = (0); i < (int((li).size())); i++) {
    dfs(li[i].first);
  }
  adj[u].clear();
}
struct Node {
  long long sum;
  int cnt;
  Node* child[3];
  Node() : sum(0), cnt(0) {}
};
Node* rt[maxn];
Node* build(int u) {
  Node* res = (Node*)malloc(sizeof(Node));
  for (int i = (0); i < (int((ct.tree[u]).size())); i++) {
    int v = ct.tree[u][i];
    res->child[i] = build(v);
  }
  return res;
}
Node* upd(int u, int v, Node* p) {
  Node* res = (Node*)malloc(sizeof(Node));
  int ix = ct.lev[v];
  res->sum = p->sum + ct.info[ix][u].d;
  res->cnt = p->cnt + 1;
  if (ix) {
    res->sum -= ct.info[ix - 1][u].d;
  }
  for (int i = (0); i < (int((ct.tree[v]).size())); i++) {
    int w = ct.tree[v][i];
    if (ct.isparent(w, u)) {
      res->child[i] = upd(u, w, p->child[i]);
    } else {
      res->child[i] = p->child[i];
    }
  }
  return res;
}
Node replace(int u, int v, int rt, Node* x, Node* y, int sig) {
  int ix = ct.lev[rt];
  x->sum += sig * ct.info[ix][u].d;
  x->cnt += sig;
  if (ix) {
    x->sum -= sig * ct.info[ix - 1][u].d;
  }
  for (int i = (0); i < (int((ct.tree[rt]).size())); i++) {
    int w = ct.tree[rt][i];
    if (ct.isparent(w, u)) {
      if (rt == v) {
        x->child[i] = y->child[i];
      } else {
        replace(u, v, w, x->child[i], y->child[i], sig);
      }
    }
  }
}
long long query(int u, int v, Node* p) {
  long long res = 0;
  int ix = ct.lev[v];
  res += p->sum;
  res += p->cnt * ct.info[ix][u].d;
  for (int i = (0); i < (int((ct.tree[v]).size())); i++) {
    int w = ct.tree[v][i];
    if (ct.isparent(w, u)) {
      res += query(u, w, p->child[i]);
      res -= p->child[i]->cnt * ct.info[ix][u].d;
    }
  }
  return res;
}
void solve() {
  cin >> n >> q;
  for (int i = (0); i < (n); i++) cin >> p[i], p[i]--;
  for (int i = (0); i < (n - 1); i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  dfs(0);
  ct.init(n);
  for (int u = (0); u < (n); u++) {
    for (int i = (0); i < (int((adj2[u]).size())); i++) {
      int v = adj2[u][i].first;
      int w = adj2[u][i].second;
      ct.add(u, v, w);
    }
    adj2[u].clear();
  }
  ct.build();
  rt[0] = build(ct.rt);
  for (int i = (0); i < (n); i++) {
    rt[i + 1] = upd(p[i], ct.rt, rt[i]);
  }
  long long ans = 0;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int a, b, c;
      cin >> a >> b >> c;
      int l = (ans % (1 << 30)) ^ a;
      int r = (ans % (1 << 30)) ^ b;
      int v = (ans % (1 << 30)) ^ c;
      v--;
      ans = query(v, ct.rt, rt[r]) - query(v, ct.rt, rt[l - 1]);
      cout << ans << "\n";
    } else {
      int a;
      cin >> a;
      int x = (ans % (1 << 30)) ^ a;
      int v = ct.lca(p[x - 1], p[x]);
      replace(p[x], v, ct.rt, rt[x], rt[x + 1], 1);
      replace(p[x - 1], v, ct.rt, rt[x], rt[x - 1], -1);
      swap(p[x - 1], p[x]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

Whoa! You did a great job helping Team Rocket who managed to capture all the Pokemons sent by Bash. Meowth, part of Team Rocket, having already mastered the human language, now wants to become a master in programming as well. He agrees to free the Pokemons if Bash can answer his questions.

Initially, Meowth gives Bash a weighted tree containing n nodes and a sequence a1, a2..., an which is a permutation of 1, 2, ..., n. Now, Mewoth makes q queries of one of the following forms:

  * 1 l r v: meaning Bash should report <image>, where dist(a, b) is the length of the shortest path from node a to node b in the given tree. 
  * 2 x: meaning Bash should swap ax and ax + 1 in the given sequence. This new sequence is used for later queries. 



Help Bash to answer the questions!

Input

The first line contains two integers n and q (1 ≤ n ≤ 2·105, 1 ≤ q ≤ 2·105) — the number of nodes in the tree and the number of queries, respectively.

The next line contains n space-separated integers — the sequence a1, a2, ..., an which is a permutation of 1, 2, ..., n.

Each of the next n - 1 lines contain three space-separated integers u, v, and w denoting that there exists an undirected edge between node u and node v of weight w, (1 ≤ u, v ≤ n, u ≠ v, 1 ≤ w ≤ 106). It is guaranteed that the given graph is a tree.

Each query consists of two lines. First line contains single integer t, indicating the type of the query. Next line contains the description of the query: 

  * t = 1: Second line contains three integers a, b and c (1 ≤ a, b, c < 230) using which l, r and v can be generated using the formula given below: 
    * <image>, 
    * <image>, 
    * <image>. 
  * t = 2: Second line contains single integer a (1 ≤ a < 230) using which x can be generated using the formula given below: 
    * <image>. 



The ansi is the answer for the i-th query, assume that ans0 = 0. If the i-th query is of type 2 then ansi = ansi - 1. It is guaranteed that: 

  * for each query of type 1: 1 ≤ l ≤ r ≤ n, 1 ≤ v ≤ n, 
  * for each query of type 2: 1 ≤ x ≤ n - 1. 



The <image> operation means bitwise exclusive OR.

Output

For each query of type 1, output a single integer in a separate line, denoting the answer to the query.

Example

Input

5 5
4 5 1 3 2
4 2 4
1 3 9
4 1 4
4 5 2
1
1 5 4
1
22 20 20
2
38
2
39
1
36 38 38


Output

23
37
28

Note

In the sample, the actual queries are the following: 

  * 1 1 5 4
  * 1 1 3 3
  * 2 3
  * 2 2
  * 1 1 3 3

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
  return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
  return a < val ? a = val, 1 : 0;
}
template <class T>
inline T isqrt(T k) {
  T r = sqrt(k) + 1;
  while (r * r > k) r--;
  return r;
}
template <class T>
inline T icbrt(T k) {
  T r = cbrt(k) + 1;
  while (r * r * r > k) r--;
  return r;
}
inline void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
struct CentroidTree {
  struct Info {
    long long d;
    Info() : d(0) {}
    Info(long long d) : d(d) {}
    Info(const Info& rhs) : d(rhs.d) {}
    Info operator+(const int& w) const { return Info(d + w); }
    Info operator+(const Info& rhs) const { return Info(d + rhs.d); }
  };
  static const int maxn = 400000 + 10;
  static const int logn = 20;
  int n, rt;
  vector<pair<int, int> > adj[maxn];
  int size[maxn];
  int rem[maxn];
  vector<int> tree[maxn];
  int lev[maxn];
  int tin[maxn];
  int tou[maxn];
  int tms;
  int p[logn][maxn];
  Info info[logn][maxn];
  void init(int n) {
    this->n = n;
    for (int i = 0; i < n; i++) {
      adj[i].clear();
      tree[i].clear();
      rem[i] = 0;
    }
    tms = 0;
  }
  void add(int u, int v, int w) {
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  void dfs(int u, int p) {
    size[u] = 1;
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (v != p && !rem[v]) {
        dfs(v, u);
        size[u] += size[v];
      }
    }
  }
  int findcentroid(int u) {
    dfs(u, -1);
    int p = -1, t = size[u];
    while (1) {
      pair<int, int> best;
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        if (v != p && !rem[v]) {
          best = max(best, make_pair(size[v], v));
        }
      }
      if (best.first + best.first <= t) {
        return u;
      }
      p = u, u = best.second;
    }
  }
  void upd(int u, int dep, int p) {
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      int w = adj[u][i].second;
      if (v != p && !rem[v]) {
        info[dep][v] = info[dep][u] + w;
        upd(v, dep, u);
      }
    }
  }
  int divide(int u, int dep) {
    lev[u = findcentroid(u)] = dep;
    tin[u] = tms++;
    p[0][u] = u;
    info[dep][u] = Info(0);
    upd(u, dep, -1);
    rem[u] = 1;
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (!rem[v]) {
        int w = divide(v, dep + 1);
        tree[u].push_back(w);
        p[0][w] = u;
      }
    }
    tou[u] = tms - 1;
    adj[u].clear();
    return u;
  }
  int lca(int u, int v) {
    if (lev[u] < lev[v]) swap(u, v);
    for (int k = logn - 1; k >= 0; k--) {
      if (lev[p[k][u]] >= lev[v]) {
        u = p[k][u];
      }
    }
    if (u == v) return u;
    for (int k = logn - 1; k >= 0; k--) {
      if (p[k][u] != p[k][v]) {
        u = p[k][u], v = p[k][v];
      }
    }
    return p[0][u];
  }
  int isparent(int u, int v) { return tin[u] <= tin[v] && tou[u] >= tou[v]; }
  Info getInfo(int u, int v) {
    int a = lca(u, v);
    int ix = lev[a];
    return info[ix][u] + info[ix][v];
  }
  void build() {
    rt = divide(0, 0);
    for (int k = 1; k < logn; k++) {
      for (int u = 0; u < n; u++) {
        p[k][u] = p[k - 1][p[k - 1][u]];
      }
    }
    for (int u = 0; u < n; u++) {
      adj[u].clear();
    }
  }
} ct;
const int maxn = 400000 + 10;
int n, q;
int p[maxn];
vector<pair<int, int> > adj[maxn];
vector<pair<int, int> > adj2[maxn];
int vis[maxn];
void dfs(int u) {
  vis[u] = 1;
  vector<pair<int, int> > li;
  for (int i = (0); i < (int((adj[u]).size())); i++) {
    int v = adj[u][i].first;
    if (!vis[v]) {
      li.push_back(adj[u][i]);
    }
  }
  if (int((li).size()) <= 2) {
    adj2[u] = li;
  } else {
    adj2[u].push_back(li[0]);
    adj2[u].push_back(make_pair(n, 0));
    for (int i = (1); i < (int((li).size()) - 1); i++) {
      adj2[n].push_back(li[i]);
      if (i < int((li).size()) - 2) {
        adj2[n].push_back(make_pair(n + 1, 0));
      } else {
        adj2[n].push_back(li[i + 1]);
      }
      n++;
    }
  }
  for (int i = (0); i < (int((li).size())); i++) {
    dfs(li[i].first);
  }
  adj[u].clear();
}
struct Node {
  long long sum;
  int cnt;
  Node* child[3];
  Node() : sum(0), cnt(0) {}
};
Node* rt[maxn];
Node* build(int u) {
  Node* res = (Node*)malloc(sizeof(Node));
  for (int i = (0); i < (int((ct.tree[u]).size())); i++) {
    int v = ct.tree[u][i];
    res->child[i] = build(v);
  }
  return res;
}
Node* upd(int u, int v, Node* p) {
  Node* res = (Node*)malloc(sizeof(Node));
  int ix = ct.lev[v];
  res->sum = p->sum + ct.info[ix][u].d;
  res->cnt = p->cnt + 1;
  if (ix) {
    res->sum -= ct.info[ix - 1][u].d;
  }
  for (int i = (0); i < (int((ct.tree[v]).size())); i++) {
    int w = ct.tree[v][i];
    if (ct.isparent(w, u)) {
      res->child[i] = upd(u, w, p->child[i]);
    } else {
      res->child[i] = p->child[i];
    }
  }
  return res;
}
Node replace(int u, int v, int rt, Node* x, Node* y, int sig) {
  int ix = ct.lev[rt];
  x->sum += sig * ct.info[ix][u].d;
  x->cnt += sig;
  if (ix) {
    x->sum -= sig * ct.info[ix - 1][u].d;
  }
  for (int i = (0); i < (int((ct.tree[rt]).size())); i++) {
    int w = ct.tree[rt][i];
    if (ct.isparent(w, u)) {
      if (rt == v) {
        x->child[i] = y->child[i];
      } else {
        replace(u, v, w, x->child[i], y->child[i], sig);
      }
    }
  }
}
long long query(int u, int v, Node* p) {
  long long res = 0;
  int ix = ct.lev[v];
  res += p->sum;
  res += p->cnt * ct.info[ix][u].d;
  for (int i = (0); i < (int((ct.tree[v]).size())); i++) {
    int w = ct.tree[v][i];
    if (ct.isparent(w, u)) {
      res += query(u, w, p->child[i]);
      res -= p->child[i]->cnt * ct.info[ix][u].d;
    }
  }
  return res;
}
void solve() {
  cin >> n >> q;
  for (int i = (0); i < (n); i++) cin >> p[i], p[i]--;
  for (int i = (0); i < (n - 1); i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  dfs(0);
  ct.init(n);
  for (int u = (0); u < (n); u++) {
    for (int i = (0); i < (int((adj2[u]).size())); i++) {
      int v = adj2[u][i].first;
      int w = adj2[u][i].second;
      ct.add(u, v, w);
    }
    adj2[u].clear();
  }
  ct.build();
  rt[0] = build(ct.rt);
  for (int i = (0); i < (n); i++) {
    rt[i + 1] = upd(p[i], ct.rt, rt[i]);
  }
  long long ans = 0;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int a, b, c;
      cin >> a >> b >> c;
      int l = (ans % (1 << 30)) ^ a;
      int r = (ans % (1 << 30)) ^ b;
      int v = (ans % (1 << 30)) ^ c;
      v--;
      ans = query(v, ct.rt, rt[r]) - query(v, ct.rt, rt[l - 1]);
      cout << ans << "\n";
    } else {
      int a;
      cin >> a;
      int x = (ans % (1 << 30)) ^ a;
      int v = ct.lca(p[x - 1], p[x]);
      replace(p[x], v, ct.rt, rt[x], rt[x + 1], 1);
      replace(p[x - 1], v, ct.rt, rt[x], rt[x - 1], -1);
      swap(p[x - 1], p[x]);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}

```