malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   struct SFountain {     size_t beauty;     size_t price;     SFountain() = default;     SFountain(size_t b, size_t p) : beauty(b), price(p) {}     static bool less_price(const SFountain &lhs, const SFountain &rhs) {       return lhs.price < rhs.price;     }     static bool less_beauty(const SFountain &lhs, const SFountain &rhs) {       return lhs.beauty < rhs.beauty;     }   };   size_t maxSum(const std::vector<SFountain> &fountains, size_t max_price) {     if (fountains.size() < 2) return 0;     size_t max_beauty_total = 0;     size_t max_beauty = 0;     std::vector<SFountain> x;     for (const SFountain &fountain : fountains) {       auto upper = std::upper_bound(x.begin(), x.end(),                                     SFountain(0, max_price - fountain.price),                                     SFountain::less_price);       if (upper != x.begin()) {         --upper;         max_beauty_total =             std::max(max_beauty_total, fountain.beauty + upper->beauty);       }       if (max_beauty < fountain.beauty) {         max_beauty = fountain.beauty;         x.push_back(fountain);       }     }     return max_beauty_total;   }   int main() {     size_t n, c, d, maxC = 0, maxD = 0;     std::cin >> n >> c >> d;     std::vector<SFountain> coins;     std::vector<SFountain> diamonds;     coins.reserve(n);     diamonds.reserve(n);     for (size_t i = 0; i < n; ++i) {       SFountain fountain;       char price_type;       std::cin >> fountain.beauty >> fountain.price >> price_type;       if (price_type ==  C  && fountain.price <= c) {         coins.push_back(fountain);         maxC = std::max(maxC, fountain.beauty);       } else if (price_type ==  D  && fountain.price <= d) {         diamonds.push_back(fountain);         maxD = std::max(maxD, fountain.beauty);       }     }     std::sort(coins.begin(), coins.end(), SFountain::less_price);     std::sort(diamonds.begin(), diamonds.end(), SFountain::less_price);     size_t maxMixed = !coins.empty() && !diamonds.empty() ? maxC + maxD : 0;     std::cout << std::max(std::max(maxSum(coins, c), maxSum(diamonds, d)),                           maxMixed)               << std::endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};   int dy[] = {-1, 1, 0, 0, -1, 1, -1, 1};   bool mark[100007];   long long int n, in = -1, prime[100007];   void seive() {     prime[++in] = 2;     for (long long int i = 3; i * i < 100007; i += 2) {       if (mark[i] == 0)         for (long long int j = i * i; j < 100007; j += 2 * i) mark[j] = 1;     }     for (long long int i = 3; i < 100007; i += 2) {       if (mark[i] == 0) prime[++in] = i;     }   }   int main() {     seive();     cin >> n;     long long int c, div = 1;     for (long long int i = 0; i <= in; i++) {       if (n % prime[i] == 0) {         c = 1;         while (n % prime[i] == 0) {           n /= prime[i];           c++;         }         div *= c;       }     }     if (n > 2) div *= 2;     cout << div << endl;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int a[200001];   vector<int> G[200001];   int res[200001];   vector<int> divisor;   vector<int> now;   int gcd(int x, int y) {     if (y == 0) return x;     return gcd(y, x % y);   }   void dfs(int v, int p) {     for (int i = 0; i < G[v].size(); i++) {       if (p != G[v][i]) {         res[G[v][i]] = gcd(res[v], a[G[v][i]]);         dfs(G[v][i], v);       }     }   }   void dfs2(int v, int p, int dist) {     for (int i = 0; i < divisor.size(); i++) {       now[i] += (a[v] % divisor[i] == 0);       if (now[i] >= dist) {         res[v] = max(res[v], divisor[i]);       }     }     for (int i = 0; i < G[v].size(); i++) {       if (p != G[v][i]) {         dfs2(G[v][i], v, dist + 1);       }     }     for (int i = 0; i < divisor.size(); i++) {       now[i] -= (a[v] % divisor[i] == 0);     }   }   int main(void) {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , &a[i]);     }     for (int i = 0; i < n - 1; i++) {       int f, t;       scanf( %d%d , &f, &t);       f--;       t--;       G[f].push_back(t);       G[t].push_back(f);     }     int tmp = a[0];     a[0] = 0;     dfs(0, -1);     a[0] = tmp;     for (int i = 1; i * i <= a[0]; i++) {       if (a[0] % i == 0) {         divisor.push_back(i);         divisor.push_back(a[0] / i);         if (i * i == a[0]) {           divisor.pop_back();         }       }     }     sort(divisor.begin(), divisor.end());     now.resize(divisor.size());     dfs2(0, -1, 0);     for (int i = 0; i < n; i++) {       printf( %d%c , res[i], i + 1 == n ?   n  :    );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;      #define int long long      void solve()   {       int n, m, k;       cin >> n >> m >> k;       vector<tuple<int, int, int>> edges(m + 2), edges_large, edges_small;       vector<int> values;       for (int i = 1; i <= m; i++)       {           int a, b, c;           cin >> a >> b >> c;           edges[i] = {a, b, c};           if (c > k)               edges_large.push_back(edges[i]);           else               edges_small.push_back(edges[i]);           values.push_back(c);       }       sort(values.begin(), values.end());       vector<int> fa(n + 2);       for (int i = 1; i <= n; i++)           fa[i] = i;       function<int(int)> find = [&](int x) -> int {           return x == fa[x] ? x : fa[x] = find(fa[x]);       };       int tot = 1;       function<void(int, int)> merge = [&](int x, int y) -> void {           x = find(x);           y = find(y);           if (x != y)               fa[x] = y, tot++;       };       for (auto [u, v, w] : edges_small)           merge(u, v);       int flag = tot == n;       if (flag)       {           int i = lower_bound(values.begin(), values.end(), k) - values.begin();           int j = i - 1;           int ans = 1e18;           if (i >= 0 && i < m)               ans = min(ans, abs(k - values[i]));           if (j >= 0 && j < m)               ans = min(ans, abs(k - values[j]));           cout << ans << endl;       }       else       {           int ans = 0;           vector<pair<int, pair<int, int>>> ve;           for (auto [x, y, z] : edges_large)               ve.push_back({z, {x, y}});           sort(ve.begin(), ve.end());           for (int i = 0; i < ve.size(); i++)           {               auto [w, pr] = ve[i];               auto [u, v] = pr;               if (find(u) != find(v))                   merge(u, v), ans += w - k;               if (tot == n)                   break;           }           cout << ans << endl;       }   }      signed main()   {       ios::sync_with_stdio(false);          int t;       cin >> t;       while (t--)       {           solve();       }   }
#include <bits/stdc++.h>   using namespace std;   long long int visited[100001];   vector<long long int> adj[100001];   long long int sum = 0;   void dfs(long long int s) {     if (visited[s] == 1) {       return;     }     sum++;     visited[s] = 1;     for (auto u : adj[s]) {       dfs(u);     }   }   long long int binpow(long long int x, long long int n) {     if (n == 0) return 1;     long long u = binpow(x, n / 2);     u = (u * u);     if (n % 2 == 1) u = (u * x);     return u;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m;     cin >> n >> m;     while (m--) {       long long int x, y;       cin >> x >> y;       adj[x].push_back(y);       adj[y].push_back(x);     }     memset(visited, 0, sizeof(visited));     ;     long long int ans = 1;     for (long long int i = 1; i <= n; i++) {       if (visited[i] == 0) {         sum = 0;         dfs(i);         long long int y = sum - 1;         long long int r = binpow(2, y);         ans = ans * r;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long dp[10010], leftx[10010], rightx[10010], l[5010], r[5010];   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     int n, x;     cin >> n >> x;     vector<int> ry;     for (int i = 0; i < n; i++) {       int a, b;       cin >> a >> b;       l[i] = a;       r[i] = b;       ry.push_back(a);       ry.push_back(b);     }     ry.push_back(x);     sort(ry.begin(), ry.end());     set<int> rt;     for (auto val : ry) {       rt.insert(val);     }     ry.clear();     for (auto val : rt) {       ry.push_back(val);     }     int ind = -1;     for (int i = 0; i < ry.size(); i++) {       if (ry[i] == x) ind = i;     }     for (int i = 0; i < ry.size(); i++) dp[i] = 1e18;     for (int i = 0; i < ry.size(); i++) {       if (ry[i] == x) dp[i] = 0;     }     for (int i = 0; i <= n; i++) {       if (i) {         for (int j = 0; j < ry.size(); j++) {           long long val = 0;           if (ry[j] < l[i - 1] || ry[j] > r[i - 1])             val = min(abs(l[i - 1] - ry[j]), abs(r[i - 1] - ry[j]));           dp[j] = min(leftx[j] + ry[j] + val, rightx[j] - ry[j] + val);         }       }       leftx[0] = dp[0] - ry[0];       for (int j = 1; j < ry.size(); j++)         leftx[j] = min(leftx[j - 1], dp[j] - ry[j]);       rightx[ry.size() - 1] = dp[ry.size() - 1] + ry[ry.size() - 1];       for (int j = (int)ry.size() - 2; j >= 0; j--)         rightx[j] = min(rightx[j + 1], dp[j] + ry[j]);     }     long long mini = 1e18;     for (int j = 0; j < ry.size(); j++) mini = min(mini, dp[j]);     cout << mini <<   n ;   }
#include <bits/stdc++.h>   bool chCount[26];   int main() {     int n, m;     std::cin >> n >> m;     int max = -1;     for (; n >= 1; --n) {       int min = 1000000000 + 10;       for (int i = 0; i < m; ++i) {         int x;         std::cin >> x;         min = std::min(x, min);       }       max = std::max(min, max);     }     std::cout << max;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int a, m;     cin >> a >> m;     a %= m;     map<int, bool> vis;     while (!vis[a]) {       vis[a] = true;       if (!a) {         cout <<  Yes n ;         return 0;       }       a *= 2;       a %= m;     }     cout <<  No n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int nMax = 100007;   int parent[nMax];   vector<int> g[nMax];   bool used[nMax];   void dfs(int root, int parent, int& cnt, int b, bool& bad) {     if (root == b) {       bad = true;     }     used[root] = true;     ++cnt;     for (int i = 0; i < g[root].size(); ++i) {       int to = g[root][i];       if (used[to] == false && to != parent) dfs(to, root, cnt, b, bad);     }   }   int main() {     int n, pos;     scanf( %d%d , &n, &pos);     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       parent[i] = a - 1;       if (a != 0) {         g[i].push_back(a - 1);         g[a - 1].push_back(i);       }     }     --pos;     int h = 1;     int t = pos;     while (parent[t] != -1) {       ++h;       t = parent[t];     }     vector<int> c;     for (int i = 0; i < n; ++i) {       int cnt = 0;       bool bad = false;       if (used[i] == false) {         dfs(i, -1, cnt, pos, bad);         if (!bad) c.push_back(cnt);       }     }     int to = (int)pow(2.0, (int)c.size());     set<int> s;     set<int>::iterator it;     for (int i = 1; i < to + 1; ++i) {       int t = i, sum = 0;       for (int j = 0; j < c.size(); ++j) {         if (t & 1) sum += c[j];         t >>= 1;       }       s.insert(sum);     }     set<int> ans;     ans.insert(h);     for (it = s.begin(); it != s.end(); ++it) ans.insert(*it + h);     for (it = ans.begin(); it != ans.end(); ++it) cout << *it << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 1;   const int size = 1 << 8, M = 1e6 + 10;   int n, q, w[N], head[N], ne[N], cnt;   void add(int f, int t) {     w[++cnt] = t;     ne[cnt] = head[f];     head[f] = cnt;   }   int fa[N], a[N], h[N];   void dfs(int x) {     for (int i = head[x]; i; i = ne[i]) {       int v = w[i];       if (fa[v]) continue;       fa[v] = x;       h[v] = h[x] + 1;       dfs(v);     }   }   int ance[N], go[M][2], id, dp[N][size];   void trie(int x) {     for (int i = 15, p = 1; i >= 0; i--) {       int w = x >> i & 1;       if (!go[p][w]) go[p][w] = ++id;       p = go[p][w];     }   }   int query(int x) {     int ans = 0;     for (int i = 15, p = 1; i >= 0; i--) {       int w = x >> i & 1;       if (go[p][w ^ 1])         ans |= (1 << i), p = go[p][w ^ 1];       else         p = go[p][w];     }     return ans;   }   int main() {     scanf( %d%d , &n, &q);     for (int i = 1; i <= n; ++i) scanf( %d , &a[i]);     for (int i = 1; i < n; ++i) {       int u, v;       scanf( %d%d , &u, &v);       add(u, v);       add(v, u);     }     fa[1] = h[1] = 1;     dfs(1);     fa[1] = 0;     for (int i = 1; i <= n; ++i) {       for (; id; id--) go[id][0] = go[id][1] = 0;       id = 1;       int p = i;       for (int j = 0; p && j < size; j++, p = fa[p]) trie(a[p] ^ (h[i] - h[p]));       ance[i] = p;       for (int j = 0; j < size; j++) dp[i][j] = query(j << 8);     }     for (int i = 1; i <= q; ++i) {       int u, v;       scanf( %d%d , &u, &v);       int p = v, dep = 0, ans = 0;       for (; h[p] - size >= h[u]; p = ance[p], dep++) ans = max(ans, dp[p][dep]);       for (; h[p] >= h[u]; p = fa[p]) ans = max(ans, a[p] ^ (h[v] - h[p]));       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int n = 10000;   long long qm[6][n + 2];   void ask(int i, long long b) {     int j = min(b, (long long)n);     vector<long long> v;     long long r = qm[i - 1][j];     for (int s = 1; s <= j; ++s) {       v.push_back(r + b);       ++r;       r += qm[i - 1][min(r + j, (long long)n)];       if (r > (long long)2e18) break;     }     printf( %d n , int(v.size()));     for (auto t : v) printf( %lld  , t);     cout << endl;     int t;     cin >> t;     if (t == -1) exit(0);     if (t == 0) ask(i - 1, b);     r = qm[i - 1][j];     for (int s = 1; s <= j; ++s) {       ++r;       --t;       if (!t) ask(i - 1, r + b);       r += qm[i - 1][min(r + j, (long long)n)];     }   }   int main() {     for (int i = 1; i <= 4; ++i) {       for (int j = 1; j <= n; ++j) {         long long r = qm[i - 1][j];         for (int s = 1; s <= j; ++s) {           ++r, r += qm[i - 1][min(r + j, (long long)n)];           if (r > (long long)2e18) break;         }         qm[i][j] = r;       }     }     ask(5, 1);   }
#include <bits/stdc++.h>   using namespace std;   void swap(int &a, int &b) {     int temp;     temp = a;     a = b;     b = temp;   }   void printarr(int a[], int n) {     for (int i = 0; i < 2 * n; i++) cout << a[i] <<    ;     cout << endl;   }   int main() {     long long int n, k, v;     cin >> n;     int a[2 * n], i, j, count = 0;     for (i = 0; i < 2 * n; i++) cin >> a[i];     for (i = 0; i < 2 * n; i += 2) {       if (a[i] != a[i + 1]) {         j = a[i];         for (k = i + 1; k < 2 * n; k++) {           if (j == a[k]) {             v = k;             while (v > i + 1) {               count++;               swap(a[v], a[v - 1]);               v--;             }           }         }       }     }     cout << count;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int add(int a, int b) { return a + b - (a + b >= MOD) * MOD; }   int mult(int a, int b) { return ((long long)a * b) % MOD; }   int n, h, w;   char s[500013];   int dX[200], dY[200];   int diffx, diffy;   int maxx, minx;   int maxy, miny;   int hasx_[1000013], hasy_[1000013];   int& hasx(int i) { return hasx_[i + 500000]; }   int& hasy(int i) { return hasy_[i + 500000]; }   void calc() {     int x = 0, y = 0;     for (int i = 0; i < n; i++) {       x += dX[s[i]];       y += dY[s[i]];       maxx = max(maxx, x);       minx = min(minx, x);       maxy = max(maxy, y);       miny = min(miny, y);       if (!hasx(x)) hasx(x) = i + 1;       if (!hasy(y)) hasy(y) = i + 1;     }     diffx = x;     diffy = y;   }   int main() {     dX[ U ] = -1;     dX[ D ] = 1;     dY[ L ] = -1;     dY[ R ] = 1;     scanf( %d%d%d %s , &n, &h, &w, &s);     calc();     if (maxx - minx + 1 <= h && maxy - miny + 1 <= w && diffx == 0 && diffy == 0)       return printf( -1 n ), 0;     int bot = -1, top = h;     int lef = -1, rig = w;     int x = 0, y = 0;     int step = 0;     int ans = 0;     while (top > bot + 1 && rig > lef + 1) {       int tryt = 1e9;       if (x + top - 1 + maxx >= h) tryt = hasx(h - (top + x - 1));       int tryb = 1e9;       if (x + bot + 1 + minx <= -1) tryb = hasx(-1 - (bot + x + 1));       int tryr = 1e9;       if (y + rig - 1 + maxy >= w) tryr = hasy(w - (rig + y - 1));       int tryl = 1e9;       if (y + lef + 1 + miny <= -1) tryl = hasy(-1 - (lef + y + 1));       if (min(min(tryt, tryb), min(tryr, tryl)) < 1e7) {         if (tryt < min(tryb, min(tryr, tryl))) {           int cur = mult(rig - lef - 1, mult(step, n) + tryt);           ans = add(ans, cur);           top -= 1;         } else if (tryb < min(tryt, min(tryr, tryl))) {           int cur = mult(rig - lef - 1, mult(step, n) + tryb);           ans = add(ans, cur);           bot += 1;         } else if (tryr < min(tryt, min(tryb, tryl))) {           int cur = mult(top - bot - 1, mult(step, n) + tryr);           ans = add(ans, cur);           rig -= 1;         } else if (tryl < min(tryt, min(tryb, tryr))) {           int cur = mult(top - bot - 1, mult(step, n) + tryl);           ans = add(ans, cur);           lef += 1;         }       } else {         x += diffx;         y += diffy;         step += 1;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   inline void R(int &xx) {     xx = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) ch = getchar();     while (ch >=  0  && ch <=  9 )       xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();   }   inline void R(long long &xx) {     xx = 0;     char ch = getchar();     while (ch <  0  || ch >  9 ) ch = getchar();     while (ch >=  0  && ch <=  9 )       xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();   }   int n, prime[100011], tot;   bool f[100011], u[100011];   vector<pair<int, int> > ans;   vector<int> tmp;   inline void init() {     for (int i = 2; i <= n; i++) {       if (!f[i]) prime[++tot] = i;       for (int j = 1; j <= tot && i * prime[j] < 100011; j++) {         f[i * prime[j]] = 1;         if (!(i % prime[j])) break;       }     }   }   int main() {     R(n);     init();     for (int i = tot; i >= 1; i--) {       int pr = prime[i];       tmp.clear();       for (int j = pr; j <= n; j += pr)         if (!u[j]) tmp.push_back(j);       if (tmp.size() & 1) swap(tmp[1], tmp[tmp.size() - 1]);       for (int t = 0; t < ((int)tmp.size() - 1); t += 2) {         ans.push_back(make_pair(tmp[t], tmp[t + 1]));         u[tmp[t]] = u[tmp[t + 1]] = 1;       }     }     printf( %d n , ans.size());     for (int i = 0; i < ans.size(); i++)       printf( %d %d n , ans[i].first, ans[i].second);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> b, c;   long long s[1000000];   long long d[1000000];   int F(long long x) {     vector<long long> a = b;     a.push_back(x);     sort(a.rbegin(), a.rend());     long long n = a.size();     s[0] = 0;     for (int i = 1; i <= n; ++i) {       s[i] = s[i - 1] + a[i - 1];       d[i] = -1;     }     int y = 1;     int pos = a.size() - 1;     while (pos >= 0 && y <= n) {       if (a[pos] > y && pos + 1 > y) {         d[y] = pos + 1;         ++y;         continue;       }       --pos;     }     for (long long i = 1; i <= n; ++i) {       if (d[i] == -1) {         d[i] = s[n] - s[i];         continue;       }       d[i] = (d[i] - i) * i + s[n] - s[d[i]];     }     if (s[n] > n * (n - 1)) {       return -1;     }     for (long long k = 1; k < n; ++k) {       if (s[k] > k * (k - 1) + d[k]) {         if (a[k - 1] > x) {           return -1;         }         return 1;       }     }     return 0;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     int n;     cin >> n;     int t = 0;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       b.push_back(x);       t += x;       t %= 2;     }     for (int i = 0; i <= n + 2; ++i) {       if (i % 2 == t) {         c.push_back(i);       }     }     int l = -1;     int r = c.size();     int pos = -1;     while (r - l > 1) {       int m = (l + r) / 2;       if (F(c[m]) == 1) {         r = m;         continue;       }       if (F(c[m]) == -1) {         l = m;         continue;       }       pos = m;       break;     }     if (pos == -1) {       cout << -1;       return 0;     }     l = -1;     r = pos + 1;     while (r - l > 1) {       int m = (l + r) / 2;       if (F(c[m]) == 0) {         r = m;       } else {         l = m;       }     }     int pos1 = r;     l = pos - 1;     r = c.size();     while (r - l > 1) {       int m = (l + r) / 2;       if (F(c[m]) == 0) {         l = m;       } else {         r = m;       }     }     for (int i = pos1; i <= l; ++i) {       cout << c[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 10;   const int MN = 1e5 + 10;   int n, a[MN << 1], b[MN << 1];   struct cmp {     bool operator()(const int &x, const int &y) {       if (a[x] + b[x] != a[y] + b[y]) return a[x] + b[x] > a[y] + b[y];       return x > y;     }   };   set<int, cmp> S;   int main() {     ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);     cin >> n;     long long ans = 0;     for (int i = 0; i < n; ++i) {       cin >> a[2 * i] >> b[2 * i] >> a[2 * i + 1] >> b[2 * i + 1];       int x = a[2 * i] + b[2 * i], y = a[2 * i + 1] + b[2 * i + 1];       if (a[2 * i] > b[2 * i + 1] || b[2 * i] > a[2 * i + 1]) {         if (x >= y)           S.insert(2 * i), S.insert(2 * i + 1);         else {           if (a[2 * i] > b[2 * i + 1])             ans += a[2 * i] - b[2 * i + 1];           else             ans += a[2 * i + 1] - b[2 * i];         }       }     }     bool turn = false;     while (!S.empty()) {       int x = *S.begin();       S.erase(S.begin());       if (!turn)         ans += a[x];       else         ans -= b[x];       turn = !turn;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline void read(T &x) {     x = 0;     char c = getchar(), f = 0;     for (; c <  0  || c >  9 ; c = getchar())       if (c ==  - ) f = 1;     for (; c >=  0  && c <=  9 ; c = getchar())       x = (x << 1) + (x << 3) + (c ^ 48);     if (f) x = -x;   }   const int P = 1e9 + 7;   int n, D, dp[3005][3005], s[3005][3005], res;   struct edge {     int to, nxt;   } e[3005];   int et, head[3005], iv[3005];   inline int ksm(int x, int q = P - 2) {     int r = 1;     for (; q; q >>= 1, x = 1ll * x * x % P)       if (q & 1) r = 1ll * r * x % P;     return r;   }   inline void adde(int x, int y) { e[++et] = (edge){y, head[x]}, head[x] = et; }   inline void dfs(int x) {     for (int i = 1; i <= n + 1; i++) dp[x][i] = 1;     for (int i = head[x]; i; i = e[i].nxt) {       dfs(e[i].to);       for (int j = 1; j <= n + 1; j++)         dp[x][j] = 1ll * dp[x][j] * s[e[i].to][j] % P;     }     for (int i = 1; i <= n + 1; i++) s[x][i] = (s[x][i - 1] + dp[x][i]) % P;   }   inline int inv(int x) { return x < 0 ? P - iv[-x] : iv[x]; }   int main() {     read(n), read(D);     for (int i = 2, f; i <= n; i++) read(f), adde(f, i);     iv[1] = 1;     for (int i = 2; i <= n + 1; i++) iv[i] = 1ll * iv[P % i] * (P - P / i) % P;     dfs(1);     for (int i = 1, w = 1; i <= n + 1;          (res += 1ll * w * s[1][i] % P) %= P, w = 1, i++)       for (int j = 1; j <= n + 1; j++)         if (i ^ j) w = 1ll * w * (D - j) % P * inv(i - j) % P;     return printf( %d n , (res + P) % P), 0;   }