#include<bits/stdc++.h>
#define int long long
#define ri register signed
#define rd(x) x=read()
using namespace std;
const int N=2e5+5;
const int M=21;
const int maxn=5e6+5;
const int mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if (ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){if (y==0) return 1;int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}

int n,m,ans;
signed main()
{
	rd(n);rd(m);
	ans=n*ksm(m,n)%mod;
	for (int l=0;l<=n-2;l++) for (int v=1;v<=m;v++)
	{
		ans-=ksm(m,n-l-2)*ksm(m-v,l)%mod*(n-1-l);
		//printf("l=%lld,v=%lld,-=%lld\n",l,v,ksm(m,n-l-2)*ksm(m-v,l));
		ans%=mod;
	}
	cout<<(ans+mod)%mod<<endl;
}
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;


using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int mod = 1e9 + 7;
int A[100001];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N;
	cin >> N;
	rep1(i, N) cin >> A[i];
	sort(A + 1, A + N + 1);

	int kotae = 1;
	rep(i, N) {
		kotae = kotae * ll(A[i + 1] - A[i] + 1) % mod;
	}

	co(kotae);


	Would you please return 0;
}