#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 1e6 + 5;
const long double pi = acos(-1);
long long a[N], b[N], n, m, cnt = 0;
long long dp[N][2];
long long powmod(long long a, long long n) {
  long long ans = 1, tem = a;
  for (long long i = n; i > 0; i >>= 1) {
    if (i % 2) ans = (ans * tem) % mod;
    tem = (tem * tem) % mod;
  }
  return ans;
}
long long calc(long long pos, long long died) {
  if (dp[pos][died] != -1) return dp[pos][died];
  if (pos == n + 1) return !died;
  if (died == 1) {
    if (a[pos] > 0 && b[pos] > 0) {
      if (a[pos] > b[pos]) return dp[pos][1] = calc(pos + 1, 0);
      if (a[pos] < b[pos]) return dp[pos][1] = 0;
      return dp[pos][1] = calc(pos + 1, 1);
    }
    if (a[pos] > 0 && b[pos] == 0)
      return dp[pos][1] =
                 ((a[pos] - 1) * calc(pos + 1, 0) + calc(pos + 1, 1)) % mod;
    if (a[pos] == 0 && b[pos] > 0)
      return dp[pos][1] =
                 ((m - b[pos]) * calc(pos + 1, 0) + calc(pos + 1, 1)) % mod;
    dp[pos][1] =
        ((m * (m - 1) / 2) % mod) * calc(pos + 1, 0) + m * calc(pos + 1, 1);
    dp[pos][1] %= mod;
    return dp[pos][1];
  }
  if (a[pos] > 0 && b[pos] > 0) return dp[pos][0] = calc(pos + 1, 0);
  if (a[pos] == 0 && b[pos] == 0)
    return dp[pos][0] = (m * m % mod) * calc(pos + 1, 0) % mod;
  return dp[pos][0] = m * calc(pos + 1, 0) % mod;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] == 0) cnt++;
  }
  for (long long i = 1; i <= n; ++i) {
    cin >> b[i];
    if (b[i] == 0) cnt++;
  }
  memset(dp, -1, sizeof dp);
  long long ans = calc(1, 1);
  ans *= powmod(m, (mod - 1 - cnt) % (mod - 1));
  ans %= mod;
  cout << ans << '\n';
}
