MOD = 10**9 + 7 class FenwickTree: def __init__(self, size): self.n = size self.tree = [0] * (self.n + 1) 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 n = int(input()) p = list(map(int, input().split())) # Precompute factorials modulo MOD fact = [1] * (n + 1) for i in range(1, n + 1): fact[i] = fact[i-1] * i % MOD # Initialize Fenwick Tree with all elements present ft = FenwickTree(n) for x in range(1, n + 1): ft.update(x, 1) result = 0 for i in range(n): x = p[i] # Number of elements less than x that are still available count = ft.query(x - 1) # Multiply by the factorial of remaining elements result = (result + count * fact[n - i - 1]) % MOD # Mark x as used ft.update(x, -1) # The rank is result + 1 (1-based indexing) print((result + 1) % MOD)