#[allow(dead_code)] const MOD: usize = 1_000000000 + 7; #[derive(Debug)] struct Combination { modular: usize, n: usize, fact: Vec, inv: Vec, invfact: Vec, } impl Combination { fn new(modular: usize, n: usize) -> Self { let mut fact = vec![0; n+1]; let mut inv = vec![0; n+1]; let mut invfact = vec![0; n+1]; inv[1] = 1; fact[0] = 1; invfact[0] = 1; for i in 2..inv.len() { inv[i] = inv[modular % i] * (modular - modular / i) % modular; } for i in 1..inv.len() { fact[i] = i * fact[i-1] % modular; invfact[i] = inv[i] * invfact[i-1] % modular; } Combination { modular, n, fact, inv, invfact, } } fn comb(&self, n: usize, r: usize) -> usize { self.fact[n] * self.invfact[n-r] % self.modular * self.invfact[r] % self.modular } } fn modpow(n: usize, r: usize) -> usize { if r == 0 { return 1; } if r == 1 { return n; } let x = modpow(n, r / 2); if r % 2 == 0 { x * x % MOD } else { x * x % MOD * n % MOD } } fn main() { let mut cin = Scanner::new(); // Σ C(n,i)*i^(n-i) let n = cin.next_usize(); let c = Combination::new(MOD, n+1); let mut ans = 0; for i in 1..(n+1) { // println!("{} {} {}", i, c.comb(n, i), modpow(i, n-i)); ans += c.comb(n, i) * modpow(i, n-i) % MOD; } println!("{}", ans % MOD); } struct Scanner { reader: std::io::Stdin, tokens: std::collections::VecDeque, } impl Scanner { fn new() -> Self { Scanner { reader: std::io::stdin(), tokens: std::collections::VecDeque::new(), } } fn next(&mut self) -> String { if self.tokens.is_empty() { let mut s = String::new(); loop { self.reader.read_line(&mut s).ok(); let s = s.trim(); if s.len() != 0 { for it in s.split_whitespace() { self.tokens.push_back(it.into()) } break; } } } self.tokens.pop_front().unwrap() } fn next_generics(&mut self) -> T where T: std::str::FromStr + std::marker::Copy, ::Err: std::fmt::Debug { self.next().parse().unwrap() } #[allow(dead_code)] fn next_i32(&mut self) -> i32 { self.next_generics::() } #[allow(dead_code)] fn next_i64(&mut self) -> i64 { self.next_generics::() } #[allow(dead_code)] fn next_u64(&mut self) -> u64 { self.next_generics::() } #[allow(dead_code)] fn next_usize(&mut self) -> usize { self.next_generics::() } #[allow(dead_code)] fn next_isize(&mut self) -> isize { self.next_generics::() } }