#include <bits/stdc++.h>
using namespace std;
int loc[200005][20], grd[20];
int tar[400005], nex[400005], len[400005], fir[200005], cnt;
int vis[200005], siz[200005], A[200005];
long long dis[200005][20];
void Add(int a, int b, int c) {
  ++cnt;
  tar[cnt] = b;
  nex[cnt] = fir[a];
  len[cnt] = c;
  fir[a] = cnt;
}
void Dfs(int u, int p, int d) {
  siz[u] = 1;
  for (int i = fir[u]; i; i = nex[i]) {
    int v = tar[i];
    if (v == p || vis[v]) continue;
    dis[v][d] = dis[u][d] + len[i];
    Dfs(v, u, d);
    siz[u] += siz[v];
  }
}
void Divide(int u, int d) {
  Dfs(u, 0, d);
  int numz = siz[u], p = -1;
  bool flg = 1;
  while (flg) {
    flg = 0;
    for (int i = fir[u]; i; i = nex[i]) {
      int v = tar[i];
      if (vis[v] || v == p) continue;
      if (siz[v] * 2 > numz) {
        p = u;
        u = v;
        flg = 1;
        break;
      }
    }
  }
  dis[u][d] = 0;
  Dfs(u, 0, d);
  grd[d] = u;
  vis[u] = d + 1;
  for (int i = 0; i <= d; i++) loc[u][i] = grd[i];
  for (int i = fir[u]; i; i = nex[i]) {
    int v = tar[i];
    if (vis[v]) continue;
    Divide(v, d + 1);
  }
}
vector<long long> sum[200005];
vector<int> num[200005];
int pos[200005][20];
int n, q, u, v, x, t, l, r;
int main() {
  long long ans = 0, msk = (1ll << 30) - 1;
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) scanf("%d", &A[i]);
  for (int i = 1; i < n; i++) {
    int u, v, x;
    scanf("%d%d%d", &u, &v, &x);
    Add(u, v, x), Add(v, u, x);
  }
  Divide(1, 0);
  for (int i = 1; i <= n; i++) {
    sum[i].push_back(0);
    num[i].push_back(-1);
  }
  for (int i = 0; i < n; i++) {
    int u = A[i];
    for (int j = 0; j < vis[u]; j++) {
      sum[loc[u][j]].push_back(sum[loc[u][j]].back() + dis[u][j] -
                               (j > 0 ? dis[u][j - 1] : 0));
      pos[u][j] = num[loc[u][j]].size();
      num[loc[u][j]].push_back(i);
    }
  }
  while (q--) {
    scanf("%d", &t);
    if (t == 1) {
      scanf("%d%d%d", &l, &r, &u);
      l = (msk & ans) ^ l;
      r = (msk & ans) ^ r;
      u = (msk & ans) ^ u;
      l--, r--;
      ans = 0;
      for (int i = 0; i < vis[u]; i++) {
        v = loc[u][i];
        int pr =
            upper_bound(num[v].begin(), num[v].end(), r) - num[v].begin() - 1;
        int pl =
            lower_bound(num[v].begin(), num[v].end(), l) - num[v].begin() - 1;
        if (pl == pr) break;
        ans += sum[v][pr] - sum[v][pl] +
               (pr - pl) * (dis[u][i] - (i > 0 ? dis[u][i - 1] : 0));
      }
      printf("%I64d\n", ans);
    } else {
      scanf("%d", &x);
      x = (msk & ans) ^ x;
      u = A[x], v = A[x - 1];
      for (int i = 0; i < max(vis[u], vis[v]); i++) {
        if (i < min(vis[u], vis[v]) && loc[u][i] == loc[v][i]) {
          sum[loc[u][i]][pos[v][i]] = sum[loc[u][i]][pos[v][i] + 1] -
                                      sum[loc[u][i]][pos[v][i]] +
                                      sum[loc[u][i]][pos[v][i] - 1];
          swap(pos[u][i], pos[v][i]);
        } else {
          if (i < vis[u]) num[loc[u][i]][pos[u][i]]--;
          if (i < vis[v]) num[loc[v][i]][pos[v][i]]++;
        }
      }
      swap(A[x], A[x - 1]);
    }
  }
}
