MOD = 10**9 + 7 class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) # 1-based indexing def update(self, idx, delta): while idx <= self.n: self.tree[idx] += delta idx += idx & -idx def query(self, idx): res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): import sys input = sys.stdin.read().split() N = int(input[0]) p = list(map(int, input[1:N+1])) # Precompute factorials modulo MOD fact = [1] * (N + 1) for i in range(1, N + 1): fact[i] = (fact[i - 1] * i) % MOD ft = FenwickTree(N) total = 0 for i in reversed(range(N)): current = p[i] # Count of numbers less than current to the right c = ft.query(current - 1) exponent = N - i - 1 term = (c * fact[exponent]) % MOD total = (total + term) % MOD ft.update(current, 1) print((total + 1) % MOD) if __name__ == "__main__": main()