#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1E9 + 7;
const long long N = 2E5 + 6;
long long n, m, a[N], b[N], ans, zeros[N];
long long dp[N];
long long modexp(long long b, long long e) {
  long long res = 1;
  while (e) {
    if (e & 1) res = (res * b) % MOD;
    e >>= 1;
    b = (b * b) % MOD;
  }
  return res;
}
long long recur(long long ind) {
  if (dp[ind] != -1) return dp[ind];
  if (ind == n + 1) {
    dp[ind] = 0;
    return 0;
  }
  long long ans = 0;
  if (a[ind] == 0 && b[ind] != 0) {
    ans = (m - b[ind] + MOD) % MOD;
    ans = (ans * modexp(m, zeros[ind + 1]) % MOD) % MOD;
    ans = (ans + recur(ind + 1)) % MOD;
  } else if (a[ind] != 0 && b[ind] == 0) {
    ans = (a[ind] - 1 + MOD) % MOD;
    ans = (ans * modexp(m, zeros[ind + 1]) % MOD) % MOD;
    ans = (ans + recur(ind + 1)) % MOD;
  } else if (a[ind] == 0 && b[ind] == 0) {
    ans = ((m * (m - 1)) % MOD * 500000004LL) % MOD;
    ans = (ans * modexp(m, zeros[ind + 1])) % MOD;
    ans = (ans + (m * recur(ind + 1)) % MOD) % MOD;
  } else {
    if (a[ind] > b[ind]) {
      ans = modexp(m, zeros[ind + 1]) % MOD;
    } else if (a[ind] == b[ind])
      ans = recur(ind + 1) % MOD;
  }
  dp[ind] = ans;
  return ans;
}
long long modinv(long long x) { return modexp(x, MOD - 2); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  if (fopen("inp.txt", "r")) {
    freopen("inp.txt", "r", stdin);
    freopen("outp.txt", "w", stdout);
  }
  cin >> n >> m;
  long long i;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) cin >> b[i];
  memset(dp, -1, sizeof(dp));
  zeros[n + 1] = 0;
  zeros[n] = (b[n] == 0) + (a[n] == 0);
  for (i = n - 1; i >= 1; i--)
    zeros[i] = zeros[i + 1] + (a[i] == 0) + (b[i] == 0);
  long long total = modexp(m, zeros[1]);
  cout << (recur(1) * modinv(total)) % MOD << endl;
  return 0;
}
