MOD = 10**9 + 7 class FenwickTree: def __init__(self, size): self.size = size self.tree = [0] * (size + 2) def update(self, index, delta): while index <= self.size: self.tree[index] += delta index += index & -index def query(self, index): res = 0 while index > 0: res += self.tree[index] index -= index & -index return res def main(): import sys input = sys.stdin.read().split() N = int(input[0]) p = list(map(int, input[1:N+1])) 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 range(N-1, -1, -1): current = p[i] smaller = ft.query(current - 1) term = smaller * fact[N - i - 1] % MOD total = (total + term) % MOD ft.update(current, 1) ans = (total + 1) % MOD print(ans) if __name__ == "__main__": main()