原题链接:https://codeforces.com/contest/1391/problem/C
A permutation of length n n n is an array consisting of n distinct integers from 1 1 1 to n n n in arbitrary order. For example, $[2,3,1,5,4] $is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 ( 2 (2 appears twice in the array ) ) ) and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 (n=3 (n=3 but there is 4 4 4 in the array ) ) ).
Consider a permutation p p p of length n n n, we build a graph of size n n n using it as follows:
For every 1 ≤ i ≤ n 1≤i≤n 1≤i≤n, find the largest j j j such that 1 ≤ j < i 1≤j1≤j<i and p j > p i p_j>p_i pj>pi, and add an undirected edge between node i i i and node j j j
For every 1 ≤ i ≤ n 1≤i≤n 1≤i≤n, find the smallest j j j such that i < j ≤ n i
In cases where no such j j j exists, we make no edges. Also, note that we make edges between the corresponding indices, not the values at those indices.
For clarity, consider as an example n = 4 n=4 n=4, and p = [ 3 , 1 , 4 , 2 ] p=[3,1,4,2] p=[3,1,4,2]; here, the edges of the graph are ( 1 , 3 ) , ( 2 , 1 ) , ( 2 , 3 ) , ( 4 , 3 ) (1,3),(2,1),(2,3),(4,3) (1,3),(2,1),(2,3),(4,3).
A permutation p p p is cyclic if the graph built using p p p has at least one simple cycle.
Given n n n, find the number of cyclic permutations of length n n n. Since the number may be very large, output it modulo 1 0 9 + 7 10^9+7 109+7.
Please refer to the Notes section for the formal definition of a simple cycle
Input
The first and only line contains a single integer n ( 3 ≤ n ≤ 1 0 6 ) n (3≤n≤10^6) n(3≤n≤106).
Output
Output a single integer 0 ≤ x < 1 0 9 + 7 0≤x<10^9+7 0≤x<109+7, the number of cyclic permutations of length n n n modulo 1 0 9 + 7 10^9+7 109+7.
Examples
input
4
output
16
input
583291
output
135712853
Note
There are 16 16 16 cyclic permutations for n = 4. n=4. n=4. [ 4 , 2 , 1 , 3 ] [4,2,1,3] [4,2,1,3] is one such permutation, having a cycle of length four : 4 → 3 → 2 → 1 → 4 : 4→3→2→1→4 :4→3→2→1→4.
Nodes v 1 , v 2 , … , v k v_1, v_2, …, v_k v1,v2,…,vk form a simple cycle if the following conditions hold:
(先上代码,题解稍后送上)
#include
#include
#include
#include
#include
#include
#define ll long long
const ll mod = 1e9 + 7;
using namespace std;
ll fun(ll n)// n!
{
ll ans = 1;
for (ll i = 1; i <= n; i++)
ans = ans * i % mod;
return ans;
}
ll quick_pow(ll a, ll b, ll c) //2^(n-1)快速幂
{
ll ans = 1;
a = a % c;
while (b)
{
if (b & 1)
ans = (ans * a) % c;
a = (a * a) % c;
b = b / 2;
}
return ans;
}
int main()
{
ll n;
cin >> n;
ll res;
res = fun(n) - pow2(2, n - 1, mod) + mod;
printf("%d\n", res % mod);
return 0;
}