#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;

int main() {
    int H, W; cin >> H >> W;
    int minA = INF;
    vvi As(H, vi(W));
    rep(i, H)rep(j, W){
        cin >> As[i][j];
        minA = min(minA, As[i][j]);
    }
    int ans = 0;
    rep(i, H)rep(j, W){
        ans += As[i][j] - minA;
    }
    cout << ans << endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long
template<typename T> void read(T &x) {
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-') f=-1;
	for(;isdigit(c);c=getchar())x=x*10+c-'0';
	x=x*f;
}
const int mod=1e9+7;
int n,p;
/*
(p-1)(p-2)^(n-1)

*/
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return res%mod;
}
signed main(){
	cin>>n>>p;
	cout<<qpow(p-2,n-1)%mod*(p-1)%mod<<endl;


	return 0;
}
