https://codeforces.com/problemset/problem/757/C 在CF提交
C. Felicity is Coming!
time limit per test
2 seconds
memory limit per test
256 megabytes
It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has n gyms. The i-th gym has gi Pokemon in it. There are m distinct Pokemon types in the Himalayan region numbered from 1 to m. There is a special evolution camp set up in the fest which claims to evolve any Pokemon. The type of a Pokemon could change after evolving, subject to the constraint that if two Pokemon have the same type before evolving, they will have the same type after evolving. Also, if two Pokemon have different types before evolving, they will have different types after evolving. It is also possible that a Pokemon has the same type before and after evolving.
Formally, an evolution plan is a permutation f of {1,2,...,m}, such that f(x)=y means that a Pokemon of type x evolves into a Pokemon of type y.
The gym leaders are intrigued by the special evolution camp and all of them plan to evolve their Pokemons. The protocol of the mountain states that in each gym, for every type of Pokemon, the number of Pokemon of that type before evolving any Pokemon should be equal the number of Pokemon of that type after evolving all the Pokemons according to the evolution plan. They now want to find out how many distinct evolution plans exist which satisfy the protocol.
Two evolution plans f1 and f2 are distinct, if they have at least one Pokemon type evolving into a different Pokemon type in the two plans, i. e. there exists an i such that f1(i)≠f2(i).
Your task is to find how many distinct evolution plans are possible such that if all Pokemon in all the gyms are evolved, the number of Pokemon of each type in each of the gyms remains the same. As the answer can be large, output it modulo 109+7.
Output
Output the number of valid evolution plans modulo
109+7.
Note
In the first case, the only possible evolution plan is:

In the second case, any permutation of
(1,2,3) is valid.
In the third case, there are two possible plans:


In the fourth case, the only possible evolution plan is:
每年的这个时候,费利西蒂就在附近,你可以看到喜马拉雅地区的人们在庆祝。喜马拉雅地区有n个健身房。第i个健身房里有gi只口袋妖怪。喜马拉雅地区有m种不同的口袋妖怪,数量从1到m不等。在这个节日里设立了一个特别的进化营,声称可以进化任何一种口袋妖怪。口袋妖怪的类型在进化后可能会发生变化,但前提是如果两个口袋妖怪在进化前具有相同的类型,则它们在进化后将具有相同类型。此外,如果两个口袋妖怪在进化前具有不同的类型,那么它们在进化后将具有不同的种类。口袋妖怪在进化前后也可能有相同的类型。
形式上,进化计划是{1,2,…,m}的置换f,使得f(x)=y意味着x型口袋妖怪进化为y型口袋妖怪。
健身房的领导者们对这个特殊的进化营很感兴趣,他们都计划进化自己的口袋妖怪。山上的协议规定,在每个健身房,对于每种类型的口袋妖怪,在进化任何口袋妖怪之前,该类型口袋妖怪的数量应等于根据进化计划进化所有口袋妖怪之后该类型口袋精灵的数量。他们现在想知道有多少不同的进化计划符合协议。
两个进化计划f1和f2是不同的,如果它们在两个计划中至少有一个口袋妖怪类型进化为不同的口袋妖怪类型,即存在一个i,使得f1(i)≠f2(i)。
你的任务是找出有多少不同的进化计划是可能的,这样,如果所有健身房中的所有口袋妖怪都进化了,那么每个健身房中每种类型的口袋妖怪的数量都保持不变。因为答案可能很大,所以将其模输出109+7。