#ifndef __SEGMENT_TREE_H__
#define __SEGMENT_TREE_H__

/* updated: 2020-07-23 */

#include <vector>
#include <functional>
#include <algorithm>
#include <limits>
#include <cassert>
#include <iostream>
#include <string>
#include <sstream>

/*-- てんぷれ --*/
struct HogeMonoid {
	using Type = int;
	static Type identity() {
		return Type();
	}
	static Type operate(const Type& left, const Type& right) {
		return Type();
	}
};

/*--------------*/

namespace Monoid {
	template <class T>
	struct Sum {
		using Type = T;
		static Type identity() {
			return T();
		}
		static Type operate(const Type& left, const Type& right) {
			return left + right;
		}
	};

	template <class T>
	struct Minimum {
		using Type = T;
		static Type identity() {
			return std::numeric_limits<Type>::max();
		}
		static Type operate(const Type& left, const Type& right) {
			return std::min(left, right);
		}
	};

	template <class T>
	struct Maximum {
		using Type = T;
		static Type identity() {
			return std::numeric_limits<Type>::lowest();
		}
		static Type operate(const Type& left, const Type& right) {
			return std::max(left, right);
		}
	};
}

/* 注意: コンストラクタの引数は size でない */
template <class Monoid>
class SegmentTree {
	using T = typename Monoid::Type;

	const int level_m;		// 段数 - 1
	const size_t size_m;	// 1 << level_m
	std::vector<T> arr_m;

	static T identity();
	static T operate(const T& left, const T& right);
	bool isValidIndex(size_t pos) const;
	
	static const int Default = -1;

public:
	explicit SegmentTree(int level);
	explicit SegmentTree(int level, const T& init_val);
	void construct();

	void update(size_t pos, const T& value);
	T query(size_t left, size_t right) const;
	T get(size_t pos) const;

	template <class Function>
	int findBoundary(Function func);

	void debug(int level = Default, int cell_width = 4) const;
};

template<class Monoid>
typename SegmentTree<Monoid>::T SegmentTree<Monoid>::identity() {
	return Monoid::identity();
}

template<class Monoid>
typename SegmentTree<Monoid>::T SegmentTree<Monoid>::operate(const T& left, const T& right) {
	return Monoid::operate(left, right);
}

template<class Monoid>
inline bool SegmentTree<Monoid>::isValidIndex(size_t pos) const {
	return pos >= 0 && pos < size_m;
}

/* 1 << level 要素の segment tree_m を作成 */
template<class Monoid>
SegmentTree<Monoid>::SegmentTree(int level)
	: level_m(level), size_m(1 << level) {
	assert(level < 32);
	arr_m.resize(size_m * 2, identity());
}

template<class Monoid>
inline SegmentTree<Monoid>::SegmentTree(int level, const T& init_val)
	: level_m(level), size_m(1 << level) {
	assert(level < 32);

	arr_m.resize(size_m * 2, init_val);
	construct();
}

/*
// 次が成立するように木を構築する, 初期化用 O(N)
// arr[i] = operate(arr[i * 2], arr[i * 2 + 1])
*/
template<class Monoid>
void SegmentTree<Monoid>::construct() {
	for (int i = size_m - 1; i > 0; --i) {
		arr_m[i] = operate(arr_m[i * 2], arr_m[i * 2 + 1]);
	}
}

/* pos 番目の要素を value に書き換える */
template<class Monoid>
void SegmentTree<Monoid>::update(size_t pos, const T& value) {
	pos += size_m;
	arr_m[pos] = value;
	pos >>= 1;
	while (pos > 0) {
		arr_m[pos] = operate(arr_m[pos * 2], arr_m[pos * 2 + 1]);
		pos >>= 1;
	}
}

/* [left, right) に存在する全要素の(指定した演算に関する)積をとる */
template<class Monoid>
typename SegmentTree<Monoid>::T SegmentTree<Monoid>::query(size_t left, size_t right) const {
	assert(left <= right);

	T tmp_left = identity();
	T tmp_right = identity();
	left += size_m;
	right += size_m;
	while (left < right) {
		if (left & 1) tmp_left = operate(tmp_left, arr_m[left++]);
		if (right & 1) tmp_right = operate(arr_m[--right], tmp_right);
		left >>= 1;
		right >>= 1;
	}

	return operate(tmp_left, tmp_right);
}

/* pos 番目の要素を直接取得 */
template<class Monoid>
typename SegmentTree<Monoid>::T SegmentTree<Monoid>::get(size_t pos) const {
	return arr_m[size_m + pos];
}

/*
// SegmentTree 上の二分探索 O(log N)
// func(query(0, i - 1)) == true かつ func(query(0, i)) == false となる i を(1つ)返す (0 <= i <= size)
*/
template<class Monoid>
template<class Function>
int SegmentTree<Monoid>::findBoundary(Function func) {
	if (func(arr_m[1])) return size_m;		// 全要素 true のときは先に除外

	T cumul = identity();
	int right = 1;
	while (right < size_m) {
		right <<= 1;
		T cumul_next = operate(cumul, arr_m[right]);
		if (func(cumul_next)) {
			++right;
			cumul = cumul_next;
		}
	}
	return right - size_m;
}

/* デバッグ出力用; level = -1 とすると全要素出力 */
template<class Monoid>
void SegmentTree<Monoid>::debug(int level, int cell_width) const {
	/* 値 value を幅 max_length になるように成形して文字列化 */
	auto to_str_fixed = [](const T& value, int max_length) -> std::string {
		std::ostringstream osst;
		osst << value;

		std::string str = osst.str();
		std::string output;
		if (str.size() > max_length) {
			for (int i = 0; i < max_length; ++i) {
				output += '#';
			}
		} else {
			const int remain_length = max_length - str.size();
			const int right_blank = remain_length / 2;
			const int left_blank = remain_length - right_blank;
			for (int i = 0; i < left_blank; ++i) {
				output += ' ';
			}
			output += str;
			for (int i = 0; i < right_blank; ++i) {
				output += ' ';
			}
		}
		return output;
	};

	if (level == Default) {			// デフォルト値への対処
		level = level_m;
	}

	if (level > level_m) {
		level = level_m;
	}

	std::vector<std::string> output_strs;

	for (int lev = 0; lev <= level; ++lev) {
		std::string output;
		const int front = size_m >> lev;

		output += '|';
		for (int i = 0; i < (1 << (level - lev)); ++i) {
			output += to_str_fixed(arr_m[front + i], cell_width);
			output += '|';
		}
		output_strs.push_back(output);

		cell_width = cell_width * 2 + 1;
	}

	reverse(output_strs.begin(), output_strs.end());
	for (const std::string& output : output_strs) {
		std::cout << output << std::endl;
	}
}

#endif
#ifndef __INTMOD_H__
#define __INTMOD_H__

/* updated: 2020-06-24 */

#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

#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 "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "Interpolation.h"
#include "IntMod.h"
#include "KDTree.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "List.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 "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RollingHash.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 "Timer.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

#include <cctype>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int n, k;
int a[200000];
int main() {
	cin >> n >> k;
	REP(i, 0, n) {
		cin >> a[i];
		--a[i];
	}


	MF base = 1;
	SegmentTree<Monoid::Sum<MF>> seg(18);
	SegmentTree<Monoid::Sum<MF>> cnt(18);
	MF sum = MF(k) * (k - 1) / 2 / 2;
	REP(i, 0, k) {
		seg.update(a[i], base);
		cnt.update(a[i], 1);
	}

	REP(i, k, n) {
		MF res = seg.query(0, a[i]);
		MF tmp = res / k / base;
		MF ttl = cnt.query(a[i], n);
		sum += tmp * (k - 1) / 2;
		sum += ttl - (1 - tmp) * (k - 1) / 2;
		base *= MF(k) / (k - 1);
		seg.update(a[i], base);
		cnt.update(a[i], 1);
	}

	cout << sum << endl;
	return 0;
}


Measured memory_bytes: 8280