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
|
#include <iostream>
#include <vector>
using namespace std;
//HELPER FUNCTIONS AND CLASSES
const int fact[1001]={1,1,2,6,24,120,720,5040,292,2628,6266,8884,6538,4938,9090,6259,74,1258,2630,9942,8707,2721,9827,5867,710,7743,1178,1785,9952,8412,2185,7693,6008,8131,6265,9128,8384,9998,9665,6676,6858,982,1216,2253,9069,7825,9705,5820,9171,9071,3235,4873,3221,594,2055,2948,4976,3436,9155,9774,6034,7822,4628,1361,7048,7805,4773,9574,577,9792,4964,2199,8223,9866,9580,8003,7808,796,2046,1522,1676,5665,4208,9026,7659,560,8132,6994,5045,8697,2184,8611,1659,4182,2835,9143,7119,60,5880,1714,1281,9297,7636,5962,9621,9505,6830,299,2271,7371,243,6959,8869,1497,539,1943,5234,1951,57,6783,3393,266,2431,8810,1677,9485,4277,2801,8283,7765,8750,5452,9167,8364,9999,8927,3225,1517,9206,8745,3446,5550,7554,9473,3160,7885,405,9500,5020,7462,8523,6077,3060,7858,9292,9259,3396,2801,2250,7505,9967,3567,7455,4318,7662,3348,5383,8338,9811,6904,2861,8895,8876,4477,8459,9296,4955,6426,3030,1992,8315,3965,1126,5918,8156,7810,1645,7405,1167,409,7661,2229,7674,46,8924,8969,6699,8786,8417,3814,2268,5553,922,7040,5159,6860,2173,9503,5245,5442,2022,6348,4838,9780,1457,3038,5753,7533,1046,8920,1028,7034,456,1618,2180,157,5461,8786,1808,3745,748,2669,8781,4545,2788,4725,4323,3837,2569,3564,4765,7567,9940,3733,215,2640,8992,9477,8658,4337,3494,6385,7900,7307,4683,3332,2397,5602,4308,4995,7797,3596,1494,2649,8853,4407,1443,5015,3082,8484,9084,42,1417,6575,290,9701,5607,2054,613,908,4065,1467,3407,3509,5863,9793,8845,6744,914,3964,8762,7964,3864,1361,9861,6965,198,8771,1931,6970,9544,735,1816,9870,8387,6250,1163,6796,1705,6481,7710,6137,3407,5649,2547,1745,1035,7871,1228,1459,6558,3648,3837,8490,8842,1641,4595,1515,6577,2321,5398,5492,2070,8834,8498,4842,5778,5828,8492,6779,3250,7480,6375,5099,2831,6016,80,7746,3725,9122,467,3805,8429,3358,7906,4670,1358,4470,9147,1477,1349,6653,6706,2577,7377,722,4070,2647,3417,9998,6677,5438,1522,7314,3525,951,7331,1855,700,5118,3482,5718,2750,2515,5088,7515,8767,456,6809,6853,801,2974,4996,2056,9504,1455,5781,3454,3733,8431,41,6434,4662,7477,8601,969,3141,7498,7049,1025,9963,1930,4607,1361,3062,9848,3905,7251,8804,6300,4152,6774,6633,3799,9656,930,5907,525,4546,8876,4013,8399,5834,4358,49,1301,6844,8742,6322,3419,3310,8695,502,2232,315,77,4321,136,886,7541,1077,5391,5031,7454,1750,5697,6019,8765,1563,6920,974,8706,9365,2964,4337,5298,7146,4851,8686,885,5663,5411,2207,3183,7692,1145,4642,2687,3490,541,9505,8713,6733,9771,5860,112,4397,9848,2464,4056,6054,435,3873,8059,8367,8774,8866,3322,3201,6186,837,9050,9929,794,9903,7522,3472,9079,8912,3037,7792,8933,497,4786,8289,5853,8041,4292,1702,2722,4453,8396,9653,4991,3457,3658,2764,5613,1592,1580,6819,8362,5476,6671,9829,4840,2427,2389,4386,2402,6177,9426,5322,7830,6545,4533,3289,7830,7844,3346,9019,5997,8034,9701,595,10001,6671,3150,6475,6998,6143,3815,2532,4522,8630,2541,7205,2379,327,5937,1724,3718,5212,4390,8103,5970,6319,3515,249,4073,688,9455,8967,4107,6815,3989,5923,4372,8944,4334,5275,5348,3804,4197,1275,8100,4226,1158,2001,7766,6345,678,7876,5910,7148,1516,8059,8397,1806,9091,1632,6459,143,7603,4980,558,3490,1825,7066,795,2557,6791,1048,2449,7112,1892,3566,4321,1691,2897,3836,8829,6029,3690,7829,7943,8220,2479,496,6727,2270,4055,1490,7405,5488,7289,5404,3945,4575,7103,3723,1979,9412,1738,5861,6274,2867,2303,4317,2915,2556,8340,7223,5503,1437,4940,7744,1636,2085,3892,5820,2490,2111,9716,4006,2160,9145,6839,3601,3371,677,715,7294,8323,8956,569,61,1879,1849,3072,8203,4311,1126,9779,1880,5690,7475,6435,8494,3155,6960,5551,4079,5535,3917,9560,4642,9605,5587,8418,2601,8023,8386,5039,5333,428,6238,9524,3451,9540,3998,542,1051,9348,3220,2869,1438,4698,7757,898,5085,4538,9499,7902,6015,7938,8287,3249,6089,6628,1290,5225,4241,8865,947,5025,6032,2754,8557,4713,2279,332,9496,5490,6569,6130,1039,5977,7402,4191,2934,1213,3662,2153,3744,2158,1873,5590,117,9917,749,7080,1938,7031,8193,5137,3526,7791,7163,6078,7529,6040,9983,1222,7383,1602,8297,5175,5051,2453,6519,2924,4191,1139,3736,8048,1728,6257,9600,5038,7586,2137,2677,8133,4361,8155,8338,4062,9807,8319,3679,1583,4500,5945,5849,3603,7997,8985,4592,8223,8149,5653,3518,3291,9894,6845,15,2398,4158,4574,3767,8193,1809,5847,2989,4072,1812,5587,8637,1375,4195,5531,3847,753,5091,8892,7375,2257,2599,5011,6375,1331,3221,5595,4791,3442,4294,7389,5331,6130,8118,4712,8909,3091,8762,3831,5329,7016,5632,785,2474,3349,8291,2982,4448,9284,7100,2346,8353,7156,2877,2866,6048,6653,7143,7138,9851,1726,1617,6512,1627,8649,9518,3631,3846,9769,6906,349,3185,1313,874,6928,4688,303,3723,6963,4068,7100,2292,1972,3267,4158,9450,1816,8669,6591,3135,2786,5763,2494,2877,7869,143,1898,2527,5844,7764,2195,5539,2133,2304,3526,3936,9726,7735,7171,7366,7119,2953,928,1711,8931,7122,1349,2567,4121,5743,4820,6168,9344,591,1649,8756,8637,3937,7210,7944,7095,2257,2951,979,161,4777,7038,9893,67,4680,2396,7711,6737,319,9539,5426,5809,4011,7995,7667,5423,9991,4350,18,7651,8032,9940,4121,6350,6725,2934,6769,9865,9525,2674,753,7211,2722,6500,9478,2958,19,8974,7728};
struct node {
int parent;
int n;
vector<int> children;
};
int count(node *A, int i) { //recursive should work as max recursion depth is 1000
if(A[i].children.empty()) {
A[i].n=1;
return(1);
}
int sum=1;
for(std::vector<int>::iterator it=A[i].children.begin(); it!=A[i].children.end(); ++it)
sum+=count(A, (*it));
A[i].n=sum;
return(sum);
}
//ALGORITHM
int F(node *A, int i) {
if(A[i].children.empty()) {
//*//cout<<i<<' '<<1<<' '<<1<<'\n';
return(1);
}
int ans=fact[A[i].n-1], div=1;
for(std::vector<int>::iterator it=A[i].children.begin(); it!=A[i].children.end(); ++it) {
ans*=F(A, (*it));
ans%=10007;
div*=fact[A[(*it)].n];
//*//cout<<"*"<<i<<' '<<*it<<' '<<fact[A[(*it)].n]<<'\n';
div%=10007;
}
//*//cout<<i<<' '<<ans<<' '<<div<<'\n';
return(ans/div);
}
int main() {
//DECLARATIONS AND IO
node *A;
int N;
cin>>N;
++N;
A=new node[N];
for(int i=2; i<N; ++i) {
int t;
cin>>t;
A[i].parent=t;
A[t].children.push_back(i);
}
//ALGORITHM
count(A, 1);
//*//for(int i=0; i<N; ++i) cout<<A[i].n<<' ';
//*//cout<<'\n';
cout<<F(A, 1);
return(0);
}
|