Statistics

Problem Statement for "SumOverPermutations"

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 int n. Anu has computed the value f(P) for each possible permutation P of numbers 1 through n. Let Z be the sum of the n factorial values Anu computed. Compute and return the value (Z modulo 1,000,000,007).

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

  1. 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.

  2. 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.

  3. 10

    Returns: 58310114

  4. 3900

    Returns: 940560814

  5. 1

    Returns: 1

  6. 2

    Returns: 4

  7. 3

    Returns: 66

  8. 4

    Returns: 2400

  9. 5

    Returns: 144080

  10. 6

    Returns: 12788160

  11. 7

    Returns: 570748361

  12. 8

    Returns: 931817742

  13. 9

    Returns: 66254011

  14. 10

    Returns: 58310114

  15. 11

    Returns: 717626691

  16. 12

    Returns: 800383063

  17. 13

    Returns: 388008777

  18. 14

    Returns: 672215095

  19. 15

    Returns: 271647317

  20. 16

    Returns: 20560194

  21. 17

    Returns: 803590225

  22. 18

    Returns: 257141587

  23. 19

    Returns: 488119813

  24. 20

    Returns: 178569840

  25. 4000

    Returns: 967156170

  26. 3999

    Returns: 94606603

  27. 3998

    Returns: 897606562

  28. 3997

    Returns: 211720744

  29. 3996

    Returns: 883181662

  30. 3995

    Returns: 374977928

  31. 3994

    Returns: 445900869

  32. 3993

    Returns: 588087221

  33. 3992

    Returns: 290709997

  34. 3991

    Returns: 75643602

  35. 3990

    Returns: 354412589

  36. 3989

    Returns: 168719591

  37. 3988

    Returns: 310104153

  38. 3987

    Returns: 938876261

  39. 3986

    Returns: 228708489

  40. 3985

    Returns: 144484356

  41. 3984

    Returns: 267936419

  42. 3983

    Returns: 261497703

  43. 3982

    Returns: 742208910

  44. 3981

    Returns: 948542496

  45. 3980

    Returns: 86758903

  46. 139

    Returns: 13915986

  47. 639

    Returns: 679412297

  48. 1139

    Returns: 39958715

  49. 1639

    Returns: 387252139

  50. 2139

    Returns: 91988108

  51. 2639

    Returns: 368927832

  52. 3139

    Returns: 672471112

  53. 3639

    Returns: 167107939

  54. 2705

    Returns: 697690186

  55. 2879

    Returns: 385329355

  56. 3689

    Returns: 314621455

  57. 2676

    Returns: 810958150

  58. 1511

    Returns: 401672378

  59. 1653

    Returns: 86157521

  60. 82

    Returns: 977723236

  61. 1720

    Returns: 159958265

  62. 2702

    Returns: 260150399

  63. 2735

    Returns: 392024346

  64. 374

    Returns: 700142039

  65. 2692

    Returns: 576367139

  66. 546

    Returns: 495614063

  67. 2592

    Returns: 840810742

  68. 1442

    Returns: 615016775

  69. 3434

    Returns: 783274669

  70. 2984

    Returns: 904646048

  71. 2664

    Returns: 357724076

  72. 2444

    Returns: 526203270

  73. 3942

    Returns: 398853892

  74. 1234

    Returns: 489060768


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: