# Codeforces 1391 C. Cyclic Permutations（组合数，推公式）

A permutation of length 𝑛 is an array consisting of 𝑛 distinct integers from 1 to 𝑛 in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (𝑛=3 but there is 4 in the array).

Consider a permutation 𝑝 of length 𝑛, we build a graph of size 𝑛 using it as follows:

For every 1≤𝑖≤𝑛, find the largest 𝑗 such that 1≤𝑗<𝑖 and 𝑝𝑗>𝑝𝑖, and add an undirected edge between node 𝑖 and node 𝑗
For every 1≤𝑖≤𝑛, find the smallest 𝑗 such that 𝑖<𝑗≤𝑛 and 𝑝𝑗>𝑝𝑖, and add an undirected edge between node 𝑖 and node 𝑗
In cases where no such 𝑗 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 𝑛=4, and 𝑝=[3,1,4,2]; here, the edges of the graph are (1,3),(2,1),(2,3),(4,3).

A permutation 𝑝 is cyclic if the graph built using 𝑝 has at least one simple cycle.

Given 𝑛, find the number of cyclic permutations of length 𝑛. Since the number may be very large, output it modulo 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 𝑛 (3≤𝑛≤106).

Output
Output a single integer 0≤𝑥<109+7, the number of cyclic permutations of length 𝑛 modulo 109+7.

Examples
inputCopy
4
outputCopy
16
inputCopy
583291
outputCopy
135712853
Note
There are 16 cyclic permutations for 𝑛=4. [4,2,1,3] is one such permutation, having a cycle of length four: 4→3→2→1→4.

Nodes 𝑣1, 𝑣2, …, 𝑣𝑘 form a simple cycle if the following conditions hold:

𝑘≥3.
𝑣𝑖≠𝑣𝑗 for any pair of indices 𝑖 and 𝑗. (1≤𝑖<𝑗≤𝑘)
𝑣𝑖 and 𝑣𝑖+1 share an edge for all 𝑖 (1≤𝑖<𝑘), and 𝑣1 and 𝑣𝑘 share an edge.

#pragma GCC optimize(2)

#include<iostream>
#include<cstdio>
#include <map>
#include<cstring>
#include <map>
#include <math.h>
#include<queue>
#include <algorithm>

using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 7;
typedef long long ll;

ll fac[maxn],p[maxn];
int main() {
int n;scanf("%d",&n);
ll ans = 1;
fac = 1;
p = 1;
for(int i = 1;i <= n;i++) {
fac[i] = fac[i - 1] * i % mod;
p[i] = p[i - 1] * 2 % mod;
}

ans = (fac[n] - p[n - 1] + mod) % mod;
printf("%lld\n",ans);
return 0;
}


08-10 307                                                                  08-10 157
08-16 114
08-10 79
01-20 1万+
08-11 887
08-10 194
08-11 77
06-27 374
09-26 1412
10-05 48
03-08 604
05-19
12-20
09-18
01-15 8028
01-15 1万+