Tree Painitng

Hello, please help me. Below is the task description, and below that is my thoughts on the problem.
Task
The tree consists of N branches numbered 1 to N. Branch 1 is the trunk and does not have a parent; 
every other branch has a “parent”, whose number is smaller than its own.
The gardener will only paint one branch at a time and to make sure he
doesn't miss a branch, he will only paint a branch if its
parent is already painted.
You have been asked to calculate the correct number of orders. Since the number is large you 
only need to find the answer modulo 10 007.

Example
If the tree has a trunk which splits into two branches (2
and 3), and 2 has a child 4 then there are three orders the
gardener can paint the tree:
• 1, 2, 3, 4
• 1, 2, 4, 3
• 1, 3, 2, 4.

Input
The first line of input contains an integer N. For i =
2, 3, . . . ,N, line i contains a single integer, the number of
the parent of branch i.

Sample input
4
1
1
2

Output
The output consists of a single line containing single integer
between 0 and 10 006, the number of ways the gardeners
can paint the tree, modulo 10 007.

Sample output
3

Constraints
N<1000

Time limit
1 second


I know trying all possible orders is too slow.And also doing a BFS into the solutions uses too much space. Do any of you have any other solutions?
it's an algorithm problem, and is it coming from an ACM / ICPC contest? if so, please give me the online judge address, otherwise I cannot give you the correct code.
briefly, you can create a tree first, which is simple.
and for each node, the way to paint it with its sub tree can be calculated by
F(n) = F(n.left) * F(n.right) * C(N(n.left), N(n.left) + N(n.right))
n.left means the left sub tree, n.right means the right sub tree, C(x, y) means the total count of methods select x elements from y elements. N is the function to return all the node count in the sub tree, include the root.
let me explain it a little bit,
so first of all, you need to paint all the nodes, but you need to paint the node n first, then left or right sub tree. since the count of nodes in left and right sub tree are constants, and you always can paint 'half of the left tree', then go to 'right tree', then finish 'the other half of the left tree', so the order between nodes in left and right tree is irrelevant. this is the reason of C(N(n.left), N(n.left) + (n.right)). but since you can paint N(n.left) nodes in F(n.left) ways, and paint N(n.right) nodes in F(n.right) ways, so you can get the final F function.

use a more complex sample as example,
6
1 1 2 3 3
it's
1
|\
2 3
| |\
4 5 6
so F(1) = F(2) * F(3) * C(N(2), N(2) + N(3))
C(N(2), N(2) + N(3)) = C(2, 5) = 5 * 4 / 2 = 10
F(2) = 1
F(3) = F(5) * F(6) * C(N(5), N(5) + N(6)) = 1 * 1 * C(1, 2) = 2 / 1 = 2
F(1) = 1 * 2 * 10 = 20

say
1 2 4 3 5 6
1 2 4 3 6 5
1 2 3 4 5 6
1 2 3 4 6 5
1 2 3 5 4 6
1 2 3 6 4 5
1 2 3 5 6 4
1 2 3 6 5 4
1 3 2 4 5 6
1 3 2 4 6 5
1 3 2 5 4 6
1 3 2 6 4 5
1 3 2 5 6 4
1 3 2 6 5 4
1 3 5 2 4 6
1 3 5 2 6 4
1 3 6 2 4 5
1 3 6 2 5 4
1 3 5 6 2 4
1 3 6 5 2 4
you will see the order of 3 5 6 and the order of 2 4 are irrelevant, so you can combine them with C function.

p.s. this is just the algorithm, you still need to do some optimize for 1 second limitation, say, prepare the tree, so you can get the result of N function in O(1), pre-compute C(x, y), attention, C(x, y) = C(y - x, y), the max value of C(500, 1000) is totally not acceptable in int, it's 1000! / 500! / 500!, but you can modulo 10007 first, since other operations are all multiplies.
Last edited on
There is a high possibility that my algorithm would be flawed but following is an interesting idea. That is if it is good enough to be explored.

Example, 1 has 3 children (2, 3, 4).
2 has 2 children (5, 6).

1. Start with the root node ( 1 ).
2. Calculate the total number of nodes in each child branch ( 3, 1, 1 ).
Lets say we mark them as x1, x2, x3, y and z. Now assuming x1, x2, x3 have already been sorted, we arrange these 5 nodes in 5 x 4 x 1 ways. = 20 ways.
3. For each child, if there is a subchild, then goto step 1 for that sub-child.

We know that node 2 has 2 children and the number of nodes in each branch is (1, 1)
So number of arrangements = 2.
Hence the total number of arrangements = 20 x 2 = 40.

EDIT: Step 2 calculation error corrected.
Last edited on
seems I have made a mistake, I thought it's binary tree only,
but if it could be a more generic tree, the function
F(n) = F(n.left) * F(n.right) * C(N(n.left), N(n.left) + N(n.right))
can almost still work,
just a little bit change to
F(n) = F(n.sub1) * F(n.sub2) * ... * F(n.subX) * C(N(n.sub1), N(n) - 1) * C(N(n.sub2), N(n) - 1 - N(n.sub1)) * ... * C(N(n.subX), N(n) - 1 - N(n.sub1) - N(n.sub2) - ... - N(n.subX-1))

so in abhishekm71's case
1
/ | \
2 3 4
| \
5 6

it should be
F(1) = F(2) * F(3) * F(4) * C(1, 5) * C(1, 4) * C(3, 3)
F(2) = F(5) * F(6) * C(1, 2) * C(1, 1) = 1 * 1 * 2 * 1 = 2
F(3) = 1, F(4) = 1
F(1) = 2 * 1 * 1 * 5 * 4 = 40
And we need to only calculate each term modulo 10007
since (a*b) % m == ( (a%m) * (b%m) ) % m.

Max value for any product is 10006*10006; arithmetic using int would suffice.

Last edited on
please give me the online judge address, otherwise I cannot give you the correct code.

The judge is private and can only be accessed by members of my training division.

so you can get the result of N function in O(1)

That can be done easily, thank you.

pre-compute C(x, y)

And store the results where and how: there are 63 001 numbers minimum to store (assuming you remove duplicates C(x, y) == C(y-x,y))

Also, thank you for your help thus far. It is really appreciated :)
Last edited on
@Hzj jie
Nice!

For the general case, actually the problem of C(N(n.sub1), N(n) - 1) * C(N(n.sub2), etc is same as:

In how many ways can n objects be arranged out which p are alike, q are alike and r are alike.
factorial(n)
= ---------------------------
fact(p) * fact(q) * fact(r)
Well I have coded this, let me know what you think.
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);
}


And the code that created the fact sequence:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
using namespace std;

int main() {
	int A[1001]={0};
	A[0]=1;
	for(int i=1; i<1001; ++i) {
		A[i]=(A[i-1]*i)%10007;
	}
	
	ofstream out("fact.txt");
	for(int i=0; i<1001; ++i) {
		out<<A[i]<<',';
	}
	out<<'\n';
	return(0);
}
oops, the fact you have mentioned in your code and reply seems not what i said.
the C is a function of combination, while this is the wikipedia page about it
http://en.wikipedia.org/wiki/Combination
so, it has two parameters, say a, b
C(a, b) means the count of methods to select a elements from b elements, a <= b. so C(a, b) = b! / a! / (b - a)!
so you need to have an 2d array, say C[1001][1001], it will use about 1M * 4 = 4M memory. but this should not be the limitation.
@Hzj jie
I do not think you read abhishekm71's suggestion.

In how many ways can n objects be arranged out which p are alike, q are alike and r are alike.
factorial(n)
= ---------------------------
fact(p) * fact(q) * fact(r)


It is the same as the one you suggested with C(x,y), however it uses far less space.
But, the question remains: What do you guys think of my code?
Well i'm not experienced enough to comment about anyone's code but I tried to run your code and it gives the correct answer for the test cases that I tried. Couple of points that come to my notice are:

1. You seem to have a memory leak in your main function.
2. Instead of dynamically allocating memory for your A array, I would have prefered to use a vector.
You seem to have a memory leak in your main function.

Problem should be solved thank you.
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);
	delete[] A;
	return(0);
}
Topic archived. No new replies allowed.