1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <bits/stdc++.h>
using namespace std;

#define mod 1000000007

typedef long long ll;

int n, q, l[5000001], zl = 1, o[5000001], vp[5000001], d1[5000001], d2[5000001];
vector <int> gr[5000001];
bool odw[5000001];

void gen()
{
    queue <pair <int, int> > q;

    q.push({1, 1});

    while(!q.empty())
    {
        int v = q.front().first, d = q.front().second; q.pop();

        vp[d] = v;

        for(int i = 0; i < l[d]; i++)
        {
            gr[v].push_back(++zl);
            o[zl] = v;

            q.push({zl, d + 1});
        }
    }
}

void dfs(int v, int pop, int lvl, int&b)
{
    if(lvl == b)
    {
        b = v;
        return;
    }

    for(auto& u : gr[v])
    {
        if(u ^ o[v] && u ^ pop)
        {
            dfs(u, v, lvl + 1, b);
            return;
        }
    }
}

void dfsr(int v, int lca, int lvl, int& b, int pop)
{
    if(lvl == lca)
    {
        dfs(v, pop, lvl, b);
        return;
    }

    dfsr(o[v], lca, lvl - 1, b, v);
}

void dfs2(int v, int pop, int d, bool co)
{
    if(co == 0)
        d1[v] = d;
    else
        d2[v] = d;

    for(auto& u : gr[v])
    {
        if(u ^ pop)
            dfs2(u, v, d + 1, co);
    }

    if(o[v] ^ pop && o[v] ^ 0)
        dfs2(o[v], v, d + 1, co);
}

ll komb(int il, bool kt)
{
    ll zw;

    if(il == zl)
        return 0;

    if(kt == 0)
    {
        zw = LLONG_MIN;

        for(int i = 1; i <= zl; i++)
        {
            if(odw[i] == 0)
            {
                odw[i] = 1;

                zw = max(zw, komb(il + 1, !kt) + ll(d1[i]));

                odw[i] = 0;
            }
        }
    }
    else
    {
        zw = LLONG_MAX;

        for(int i = 1; i <= zl; i++)
        {
            if(odw[i] == 0)
            {
                odw[i] = 1;

                zw = min(zw, komb(il + 1, !kt) - ll(d2[i]));

                odw[i] = 0;
            }
        }
    }

    return zw;
}

int main()
{
    scanf("%d%d", &n, &q);

    for(int i = 1; i <= n - 1; i++)
        scanf("%d", &l[i]);

    gen();

    for(int qu = 0; qu < q; qu++)
    {
        int a, b, c, w1 = 0, w2 = 0;
        bool czm = 0, ct = 0;

        scanf("%d%d%d", &a, &b, &c);

        if(a < b)
        {
            swap(a, b);
            czm = 1;
        }

        dfsr(vp[a], c, a, b, 0);
        a = vp[a];

        if(czm)
            swap(a, b);

        dfs2(a, 0, 0, 0);
        dfs2(b, 0, 0, 1);

        printf("%d\n", int((komb(0, 0) + ll(mod) * ll(1000000)) % mod));
    }
}