Problem Statement
Anu is a little child. His teacher asked him to solve the following problem:
There are n bins in a row. The bins are numbered 1 through n from the left to the right. There is also an infinite supply of balls of n distinct colors. Place exactly one ball into each bin, with the restriction that adjacent bins cannot contain balls of the same color. How many valid configurations of balls in bins are there?
Anu solved this problem correctly: "There are n * (n-1)^(n-1) valid configurations. Imagine that we are filling the bins from the left to the right. I have n possibilities for the color of the ball in the leftmost bin, and then (n-1) possibilities for each following ball, because I cannot use the color of the previous ball I placed."
However, Anu's teacher then tried to confuse Anu: "Sure, but what would happen if you were to fill the bins in a different order? Wouldn't that change the answer?"
Sadly, this time Anu got confused and answered incorrectly: "Yes, it would change the answer. For example, suppose that n = 3. If we fill the bins in the order {1, 2, 3}, we have 3*2*2 = 12 ways to fill them. However, suppose we fill them in the order {1, 3, 2}. Here we have 3 possibilities for the color of the ball in bin 1, then 3 possibilities for the color of the ball in bin 3, and then only 1 possibility for the color of the ball in bin 2 -- as there are already two forbidden colors when filling in bin 2. Thus, for this order there are only 3*3*1 = 9 ways to fill the bins."
Hopefully you can see where Anu made the mistake, but it is not really important for this problem.
In this problem we will focus on the (incorrect) method Anu used to count the number of ways to fill the bins, given the order in which to fill them. We can formally define this method as follows: Let P be a permutation of n bins. We will use f(P) to denote the value computed by Anu if the bins are filled in the order given by P. Anu will compute the value f(P) as follows: At the beginning, he sets f(P) to 1. Whenever he fills a bin, he multiplies f(P) by (n-x), where x is the number of neighboring bins that have already been filled. For example, if he has to put a ball into bin 7 in a situation where bins 6 and 8 already contain balls, he will multiply f(P) by (n-2).
You are given an
Definition
- Class:
- SumOverPermutations
- Method:
- findSum
- Parameters:
- int
- Returns:
- int
- Method signature:
- int findSum(int n)
- (be sure your method is public)
Constraints
- n will be between 1 and 4000, inclusive.
Examples
2
Returns: 4
There are two possible permutations: {1, 2} and {2, 1}. For each of them Anu will compute the value f(P) = 2*1 = 2. The answer is ((2 + 2) mod (10^9 + 7)) = 4.
3
Returns: 66
Now there are six permutations. For four of them f returns 3*2*2 = 12. These are the permutations {1, 2, 3}, {2, 1, 3}, {2, 3, 1}, and {3, 2, 1}. For the remaining two permutations f returns 3*3*1 = 9. Thus, the answer is 4*12 + 2*9 = 66.
10
Returns: 58310114
3900
Returns: 940560814
1
Returns: 1
2
Returns: 4
3
Returns: 66
4
Returns: 2400
5
Returns: 144080
6
Returns: 12788160
7
Returns: 570748361
8
Returns: 931817742
9
Returns: 66254011
10
Returns: 58310114
11
Returns: 717626691
12
Returns: 800383063
13
Returns: 388008777
14
Returns: 672215095
15
Returns: 271647317
16
Returns: 20560194
17
Returns: 803590225
18
Returns: 257141587
19
Returns: 488119813
20
Returns: 178569840
4000
Returns: 967156170
3999
Returns: 94606603
3998
Returns: 897606562
3997
Returns: 211720744
3996
Returns: 883181662
3995
Returns: 374977928
3994
Returns: 445900869
3993
Returns: 588087221
3992
Returns: 290709997
3991
Returns: 75643602
3990
Returns: 354412589
3989
Returns: 168719591
3988
Returns: 310104153
3987
Returns: 938876261
3986
Returns: 228708489
3985
Returns: 144484356
3984
Returns: 267936419
3983
Returns: 261497703
3982
Returns: 742208910
3981
Returns: 948542496
3980
Returns: 86758903
139
Returns: 13915986
639
Returns: 679412297
1139
Returns: 39958715
1639
Returns: 387252139
2139
Returns: 91988108
2639
Returns: 368927832
3139
Returns: 672471112
3639
Returns: 167107939
2705
Returns: 697690186
2879
Returns: 385329355
3689
Returns: 314621455
2676
Returns: 810958150
1511
Returns: 401672378
1653
Returns: 86157521
82
Returns: 977723236
1720
Returns: 159958265
2702
Returns: 260150399
2735
Returns: 392024346
374
Returns: 700142039
2692
Returns: 576367139
546
Returns: 495614063
2592
Returns: 840810742
1442
Returns: 615016775
3434
Returns: 783274669
2984
Returns: 904646048
2664
Returns: 357724076
2444
Returns: 526203270
3942
Returns: 398853892
1234
Returns: 489060768