#ifndef __INTMOD_H__
#define __INTMOD_H__

/* updated: 2019-10-10 */

#include <iostream>
#include <cassert>
#include <vector>
#include <cstdint>
#include <functional>
#include <unordered_map>

template <uint32_t Mod>
class IntMod {
	static_assert(Mod != 0, "Mod must not be 0");
	static_assert(Mod < 0x80000000, "Mod must be less than 0x80000000");

private:
	uint32_t value_m;

public:
	IntMod();
	IntMod(int32_t value);
	IntMod(uint32_t value);
	IntMod(int64_t value);
	IntMod(uint64_t value);

	const IntMod<Mod>& operator+() const;
	IntMod<Mod> operator-() const;
	IntMod<Mod>& operator++();
	IntMod<Mod>& operator--();
	IntMod<Mod> operator++(int dummy);
	IntMod<Mod> operator--(int dummy);
	IntMod<Mod>& operator+=(const IntMod<Mod>& right);
	IntMod<Mod>& operator-=(const IntMod<Mod>& right);
	IntMod<Mod>& operator*=(const IntMod<Mod>& right);
	IntMod<Mod>& operator/=(const IntMod<Mod>& right);
	IntMod<Mod> operator[](uint64_t exp) const;

	static IntMod<Mod> add(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> subtract(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> multiply(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static IntMod<Mod> divide(const IntMod<Mod>& a, const IntMod<Mod>& b);
	static int compare(const IntMod<Mod>& a, const IntMod<Mod>& b);

	IntMod<Mod> inverse() const;
	IntMod<Mod> power(uint64_t exp) const;
	uint32_t get() const;

	static IntMod<Mod> fact(uint32_t num);
	static IntMod<Mod> invFact(uint32_t num);
	static IntMod<Mod> combi(uint32_t n, uint32_t r);
	static IntMod<Mod> homcombi(uint32_t n, uint32_t r);
	static IntMod<Mod> perm(uint32_t n, uint32_t r);
	static std::vector<IntMod<Mod>> calcInverseList(int max);
};

template <uint32_t Mod>
IntMod<Mod> operator+(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator-(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator*(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
IntMod<Mod> operator/(const IntMod<Mod>& a, const IntMod<Mod>& b);

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const IntMod<Mod>& a, const Int& b);

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const Int& a, const IntMod<Mod>& b);
template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const Int& a, const IntMod<Mod>& b);

template <uint32_t Mod>
bool operator==(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator!=(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator<(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator<=(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator>(const IntMod<Mod>& a, const IntMod<Mod>& b);
template <uint32_t Mod>
bool operator>=(const IntMod<Mod>& a, const IntMod<Mod>& b);

template <uint32_t Mod, class Int>
bool operator==(const IntMod<Mod>& a, const Int& b);
template <uint32_t Mod, class Int>
bool operator!=(const IntMod<Mod>& a, const Int& b);

template <uint32_t Mod>
std::istream& operator>>(std::istream& ist, IntMod<Mod>& object);
template <uint32_t Mod>
std::ostream& operator<<(std::ostream& ost, const IntMod<Mod>& object);

using MInt = IntMod<1000000007>;
using MF = IntMod<998244353>;

// for unordered_set/map
namespace std {
	template <uint32_t Mod>
	struct hash<IntMod<Mod>> {
		size_t operator() (const IntMod<Mod>& object) const {
			return std::hash<uint32_t>()(object.get());
		}
	};
}

/*------------------------------------*/
/*-------   Implementations   --------*/
/*------------------------------------*/

template <uint32_t Mod>
IntMod<Mod>::IntMod() 
	: value_m(0) {
}

template <uint32_t Mod>
IntMod<Mod>::IntMod(int32_t value) {
	int32_t tmp = value % int32_t(Mod);
	value_m = tmp >= 0 ? tmp : Mod - uint32_t(-tmp);
}

template <uint32_t Mod>
IntMod<Mod>::IntMod(uint32_t value)
	: value_m(value % Mod) {
}

template <uint32_t Mod>
IntMod<Mod>::IntMod(int64_t value) {
	int32_t tmp = value % int64_t(Mod);
	value_m = tmp >= 0 ? tmp : Mod - uint32_t(-tmp);
}

template <uint32_t Mod>
IntMod<Mod>::IntMod(uint64_t value)
	: value_m(value % Mod) {
}

template <uint32_t Mod>
const IntMod<Mod>& IntMod<Mod>::operator+() const {
	return *this;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::operator-() const {
	IntMod<Mod> ret;
	if (value_m == 0) {
		ret.value_m = 0;
	} else {
		ret.value_m = Mod - value_m;
	}
	return ret;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator++() {
	++value_m;
	if (value_m == Mod) {
		value_m = 0;
	}
	return *this;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator--() {
	if (value_m == 0) {
		value_m = Mod;
	}
	--value_m;
	return *this;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::operator++(int dummy) {
	IntMod ret(*this);
	++*this;
	return ret;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::operator--(int dummy) {
	IntMod ret(*this);
	--*this;
	return ret;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator+=(const IntMod<Mod>& right) {
	value_m += right.value_m;		// note: value_m < 0x80000000
	if (value_m >= Mod) {
		value_m -= Mod;
	}
	return *this;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator-=(const IntMod<Mod>& right) {
	if (value_m < right.value_m) {
		value_m += Mod;				// note: value_m < 0x80000000
	}
	value_m -= right.value_m;
	return *this;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator*=(const IntMod<Mod>& right) {
	value_m = (uint64_t(value_m) * right.value_m) % Mod;
	return *this;
}

template <uint32_t Mod>
IntMod<Mod>& IntMod<Mod>::operator/=(const IntMod<Mod>& right) {
	*this *= right.inverse();
	return *this;
}

// for power
template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::operator[](uint64_t exp) const {
	return power(exp);
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::add(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret += b;
	return ret;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::subtract(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret -= b;
	return ret;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::multiply(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret *= b;
	return ret;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::divide(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	IntMod<Mod> ret(a);
	ret /= b;
	return ret;
}

template <uint32_t Mod>
int IntMod<Mod>::compare(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	if (a.value_m < b.value_m) return -1;
	if (a.value_m > b.value_m) return 1;
	return 0;
}

/* Mod must be a prime. */
template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::inverse() const {
	assert(value_m != 0);
	return power(Mod - 2);
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::power(uint64_t exp) const {
	IntMod<Mod> product(1);
	IntMod<Mod> factor(*this);
	while (exp > 0) {
		if (exp & 1) {
			product *= factor;
		}
		factor *= factor;
		exp >>= 1;
	}
	return product;
}

template <uint32_t Mod>
uint32_t IntMod<Mod>::get() const {
	return value_m;
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::fact(uint32_t num) {
	assert(num <= 0x80000000);

	static std::vector<IntMod<Mod>> table(1, 1);
	if (table.size() > num) return table[num];

	uint32_t old_size = table.size();
	table.resize(num + 1);
	for (uint32_t i = old_size; i <= num; ++i) {
		table[i] = table[i - 1] * i;
	}
	return table[num];
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::invFact(uint32_t num) {
	assert(num <= 0x80000000);

	static std::vector<IntMod<Mod>> table(1, 1);
	if (table.size() > num) return table[num];

	uint32_t old_size = table.size();
	table.resize(num + 1);
	table[num] = fact(num).inverse();
	for (uint32_t i = num; i >= old_size; --i) {
		table[i - 1] = table[i] * i;
	}
	return table[num];
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::combi(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) * invFact(n - r) * invFact(r);
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::homcombi(uint32_t n, uint32_t r) {
	assert(n != 0 || r == 0);

	if (n == 0 && r == 0) return IntMod<Mod>(1);
	return combi(n + r - 1, r);
}

template <uint32_t Mod>
IntMod<Mod> IntMod<Mod>::perm(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) * invFact(n - r);
}

template <uint32_t Mod>
std::vector<IntMod<Mod>> IntMod<Mod>::calcInverseList(int max) {
	assert(max < Mod);

	std::vector<IntMod<Mod>> table(max + 1);
	table[1] = 1;
	for (int i = 2; i <= max; ++i) {
		table[i] = (uint64_t(Mod - Mod / i) * table[Mod % i].get()) % Mod;
	}
	return table;
}

template <uint32_t Mod>
IntMod<Mod> operator+(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::add(a, b);
}

template <uint32_t Mod>
IntMod<Mod> operator-(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::subtract(a, b);
}

template <uint32_t Mod>
IntMod<Mod> operator*(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::multiply(a, b);
}

template <uint32_t Mod>
IntMod<Mod> operator/(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::divide(a, b);
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const IntMod<Mod>& a, const Int& b) {
	return a + IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const IntMod<Mod>& a, const Int& b) {
	return a - IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const IntMod<Mod>& a, const Int& b) {
	return a * IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const IntMod<Mod>& a, const Int& b) {
	return a / IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator+(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) + b;
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator-(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) - b;
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator*(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) * b;
}

template <uint32_t Mod, class Int>
IntMod<Mod> operator/(const Int& a, const IntMod<Mod>& b) {
	return IntMod<Mod>(a) / b;
}

template <uint32_t Mod>
bool operator==(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) == 0;
}

template <uint32_t Mod>
bool operator!=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) != 0;
}

template <uint32_t Mod>
bool operator<(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) < 0;
}

template <uint32_t Mod>
bool operator<=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) <= 0;
}

template <uint32_t Mod>
bool operator>(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) > 0;
}

template <uint32_t Mod>
bool operator>=(const IntMod<Mod>& a, const IntMod<Mod>& b) {
	return IntMod<Mod>::compare(a, b) >= 0;
}

template <uint32_t Mod, class Int>
bool operator==(const IntMod<Mod>& a, const Int& b) {
	return a == IntMod<Mod>(b);
}

template <uint32_t Mod, class Int>
bool operator!=(const IntMod<Mod>& a, const Int& b) {
	return a != IntMod<Mod>(b);
}

template <uint32_t Mod>
std::istream& operator>>(std::istream& ist, IntMod<Mod>& object) {
	uint64_t value;
	ist >> value;
	object = IntMod<Mod>(value);
	return ist;
}

template <uint32_t Mod>
std::ostream& operator<<(std::ostream& ost, const IntMod<Mod>& object) {
	ost << object.get();
	return ost;
}

#endif

#ifndef __RUNTIME_MOD_H__
#define __RUNTIME_MOD_H__

/* updated: 2020-06-23 */

#ifdef ONLY_MY_ENVIR
#include "IntMod.h"
#endif

/* 実行時に mod が与えられるときのための IntMod, 内容は IntMod と同じ */
/* mod の変更は禁止: fact() などが動作しない */
template <>
class IntMod<0> {
	uint32_t value_m;

	static uint32_t mod_m;
public:
	IntMod();
	IntMod(int32_t value);
	IntMod(uint32_t value);
	IntMod(int64_t value);
	IntMod(uint64_t value);

	const IntMod<0>& operator+() const;
	IntMod<0> operator-() const;
	IntMod<0>& operator++();
	IntMod<0>& operator--();
	IntMod<0> operator++(int dummy);
	IntMod<0> operator--(int dummy);
	IntMod<0>& operator+=(const IntMod<0>& right);
	IntMod<0>& operator-=(const IntMod<0>& right);
	IntMod<0>& operator*=(const IntMod<0>& right);
	IntMod<0>& operator/=(const IntMod<0>& right);
	IntMod<0> operator[](uint64_t exp) const;

	static IntMod<0> add(const IntMod<0>& a, const IntMod<0>& b);
	static IntMod<0> subtract(const IntMod<0>& a, const IntMod<0>& b);
	static IntMod<0> multiply(const IntMod<0>& a, const IntMod<0>& b);
	static IntMod<0> divide(const IntMod<0>& a, const IntMod<0>& b);
	static int compare(const IntMod<0>& a, const IntMod<0>& b);

	static void setDefaultMod(uint32_t mod);

	IntMod<0> inverse() const;
	IntMod<0> power(uint64_t exp) const;
	uint32_t get() const;

	static IntMod<0> fact(uint32_t num);
	static IntMod<0> invFact(uint32_t num);
	static IntMod<0> combi(uint32_t n, uint32_t r);
	static IntMod<0> homcombi(uint32_t n, uint32_t r);
	static IntMod<0> perm(uint32_t n, uint32_t r);
	static std::vector<IntMod<0>> calcInverseList(int max);
};

// グローバルの演算子はテンプレート版があれば動作するので特殊化をしない

using RuntimeMod = IntMod<0>;

#endif
#ifdef ONLY_MY_ENVIR
#include "RuntimeMod.h"
#endif

uint32_t IntMod<0>::mod_m = 1;

IntMod<0>::IntMod()
	: value_m(0) {
}

IntMod<0>::IntMod(int32_t value) {
	int32_t tmp = value % int32_t(mod_m);
	value_m = tmp >= 0 ? tmp : mod_m - uint32_t(-tmp);
}

IntMod<0>::IntMod(uint32_t value)
	: value_m(value % mod_m) {
}

IntMod<0>::IntMod(int64_t value) {
	int32_t tmp = value % int64_t(mod_m);
	value_m = tmp >= 0 ? tmp : mod_m - uint32_t(-tmp);
}

IntMod<0>::IntMod(uint64_t value)
	: value_m(value % mod_m) {
}

const IntMod<0>& IntMod<0>::operator+() const {
	return *this;
}

IntMod<0> IntMod<0>::operator-() const {
	IntMod<0> ret;
	if (value_m == 0) {
		ret.value_m = 0;
	} else {
		ret.value_m = mod_m - value_m;
	}
	return ret;
}

IntMod<0>& IntMod<0>::operator++() {
	++value_m;
	if (value_m == mod_m) {
		value_m = 0;
	}
	return *this;
}

IntMod<0>& IntMod<0>::operator--() {
	if (value_m == 0) {
		value_m = mod_m;
	}
	--value_m;
	return *this;
}

IntMod<0> IntMod<0>::operator++(int dummy) {
	IntMod ret(*this);
	++*this;
	return ret;
}

IntMod<0> IntMod<0>::operator--(int dummy) {
	IntMod ret(*this);
	--*this;
	return ret;
}

IntMod<0>& IntMod<0>::operator+=(const IntMod<0>& right) {
	value_m += right.value_m;		// note: value_m < 0x80000000
	if (value_m >= mod_m) {
		value_m -= mod_m;
	}
	return *this;
}

IntMod<0>& IntMod<0>::operator-=(const IntMod<0>& right) {
	if (value_m < right.value_m) {
		value_m += mod_m;				// note: value_m < 0x80000000
	}
	value_m -= right.value_m;
	return *this;
}

IntMod<0>& IntMod<0>::operator*=(const IntMod<0>& right) {
	value_m = (uint64_t(value_m) * right.value_m) % mod_m;
	return *this;
}

IntMod<0>& IntMod<0>::operator/=(const IntMod<0>& right) {
	*this *= right.inverse();
	return *this;
}

// for power
IntMod<0> IntMod<0>::operator[](uint64_t exp) const {
	return power(exp);
}

IntMod<0> IntMod<0>::add(const IntMod<0>& a, const IntMod<0>& b) {
	IntMod<0> ret(a);
	ret += b;
	return ret;
}

IntMod<0> IntMod<0>::subtract(const IntMod<0>& a, const IntMod<0>& b) {
	IntMod<0> ret(a);
	ret -= b;
	return ret;
}

IntMod<0> IntMod<0>::multiply(const IntMod<0>& a, const IntMod<0>& b) {
	IntMod<0> ret(a);
	ret *= b;
	return ret;
}

IntMod<0> IntMod<0>::divide(const IntMod<0>& a, const IntMod<0>& b) {
	IntMod<0> ret(a);
	ret /= b;
	return ret;
}

int IntMod<0>::compare(const IntMod<0>& a, const IntMod<0>& b) {
	if (a.value_m < b.value_m) return -1;
	if (a.value_m > b.value_m) return 1;
	return 0;
}

void IntMod<0>::setDefaultMod(uint32_t mod) {
	mod_m = mod;
}

/* mod_m must be a prime. */
IntMod<0> IntMod<0>::inverse() const {
	assert(value_m != 0);
	return power(mod_m - 2);
}

IntMod<0> IntMod<0>::power(uint64_t exp) const {
	IntMod<0> product(1);
	IntMod<0> factor(*this);
	while (exp > 0) {
		if (exp & 1) {
			product *= factor;
		}
		factor *= factor;
		exp >>= 1;
	}
	return product;
}

uint32_t IntMod<0>::get() const {
	return value_m;
}

IntMod<0> IntMod<0>::fact(uint32_t num) {
	assert(num <= 0x80000000);

	static std::vector<IntMod<0>> table(1, 1);
	if (table.size() > num) return table[num];

	uint32_t old_size = table.size();
	table.resize(num + 1);
	for (uint32_t i = old_size; i <= num; ++i) {
		table[i] = table[i - 1] * i;
	}
	return table[num];
}


IntMod<0> IntMod<0>::invFact(uint32_t num) {
	assert(num <= 0x80000000);

	static std::vector<IntMod<0>> table(1, 1);
	if (table.size() > num) return table[num];

	uint32_t old_size = table.size();
	table.resize(num + 1);
	table[num] = fact(num).inverse();
	for (uint32_t i = num; i >= old_size; --i) {
		table[i - 1] = table[i] * i;
	}
	return table[num];
}


IntMod<0> IntMod<0>::combi(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) * invFact(n - r) * invFact(r);
}

IntMod<0> IntMod<0>::homcombi(uint32_t n, uint32_t r) {
	assert(n != 0 || r == 0);

	if (n == 0 && r == 0) return IntMod<0>(1);
	return combi(n + r - 1, r);
}

IntMod<0> IntMod<0>::perm(uint32_t n, uint32_t r) {
	assert(n >= r);
	return fact(n) / fact(n - r);
}

std::vector<IntMod<0>> IntMod<0>::calcInverseList(int max) {
	assert(max < mod_m);

	std::vector<IntMod<0>> table(max + 1);
	table[1] = 1;
	for (int i = 2; i <= max; ++i) {
		table[i] = (uint64_t(mod_m - mod_m / i) * table[mod_m % i].get()) % mod_m;
	}
	return table;
}

#ifndef __BIT_H__
#define __BIT_H__

/* updated: 2019-10-03 */

#include <vector>
#include <cstdint>

class BinaryIndexedTree {
	using T = uint64_t;

private:
	const int size_m;
	std::vector<T> arr_m;

	static const int Default = -1;

public:
	BinaryIndexedTree(int size);
	void add(int pos, T increment);
	T sum(int left) const;
	T sum(int left, int right) const;
	T get(int pos) const;

	void debug(int size = Default) const;
};

#endif

#ifdef ONLY_MY_ENVIR
#include "BIT.h"
#endif

#include <cassert>
#include <iostream>

/* 長さ size の BIT を作成 */
BinaryIndexedTree::BinaryIndexedTree(int size)
	: size_m(size), arr_m(size) {
}

/* pos 番目の要素を increment 増加させる O(log n) */
void BinaryIndexedTree::add(int pos, T increment) {
	for (int i = pos; i != 0; i &= ~(-i)) {
		arr_m[i] += increment;
	}
	arr_m[0] += increment;
}

/* [left, ∞) に存在する全要素の和をとる O(log n) */
BinaryIndexedTree::T BinaryIndexedTree::sum(int left) const {
	if (left == 0) return arr_m[0];		// 0番目の要素は例外

	T sum = T();
	for (int i = left; i < size_m; i += i & -i) {
		sum += arr_m[i];
	}
	return sum;
}

/* [left, right) に存在する全要素の和をとる O(log n) */
BinaryIndexedTree::T BinaryIndexedTree::sum(int left, int right) const {
	assert(left <= right);

	return sum(left) - sum(right);
}

/* pos 番目の要素を直接取得 O(log n) */
BinaryIndexedTree::T BinaryIndexedTree::get(int pos) const {
	return sum(pos, pos + 1);
}

/* デバッグ出力用; size = -1 とすると全要素出力 */
void BinaryIndexedTree::debug(int size) const {
	if (size == Default) {			// デフォルト値への対処
		size = size_m;
	}

	if (size > size_m) {
		size = size_m;
	}

	std::cout << "{ ";
	for (int i = 0; i < size; ++i) {
		std::cout << arr_m[i] << ' ';
	}
	std::cout << "}" << std::endl;
}
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

int N;
int P[500000];
int main() {
	RuntimeMod::setDefaultMod(1000000007);

	cin >> N;
	REP(i, 0, N) {
		cin >> P[i];
		--P[i];
	}

	vector<int> is_hole(N, 1);
	REP(i, 0, N) if (P[i] != -1) is_hole[P[i]] = 0;
	const int K = count(is_hole.begin(), is_hole.end(), 1);
	vector<int> accum(N + 1);
	partial_sum(is_hole.begin(), is_hole.end(), accum.begin() + 1);

	RuntimeMod hole_sum = 0;
	REP(i, 0, N) if (is_hole[i]) hole_sum += i;

	int hole_cnt = 0;
	RuntimeMod coeff = 0;
	RuntimeMod ans = 0;
	BinaryIndexedTree bit(N);
	REP(i, 0, N) {
		RuntimeMod sub = 0;
		if (P[i] == -1) {
			sub += hole_sum * RuntimeMod::fact(K - 1);
			sub -= coeff * RuntimeMod::fact(K - 1);
			sub -= hole_cnt * RuntimeMod::fact(K) / 2;
			++hole_cnt;
		} else {
			int s = P[i];
			sub += s * RuntimeMod::fact(K);
			sub -= bit.sum(0, s) * RuntimeMod::fact(K);
			if (K > 0) sub -= RuntimeMod(accum[s]) * hole_cnt * RuntimeMod::fact(K - 1);
			bit.add(s, 1);
			coeff += accum[N] - accum[s];
		}
		ans += sub * RuntimeMod::fact(N - 1 - i);
	}
	cout << ans + RuntimeMod::fact(K) << endl;
	return 0;
}




Measured memory_bytes: 16916