#include <bits/stdc++.h>
using namespace std;
long long dp[100100][2];
int n;
long long m, mm;
int a[100100], b[100100];
long long md = 1000 * 1000 * 1000 + 7;
long long state(int i, int lg) {
  if (i == n) {
    if (lg) return 1LL;
    return 0LL;
  }
  if (dp[i][lg] != -1) return dp[i][lg];
  long long c = 0LL;
  if (lg) {
    c = 1LL;
    if (!a[i]) c *= m, c %= md;
    if (!b[i]) c *= m, c %= md;
    c *= state(i + 1, lg);
    c %= md;
    return dp[i][lg] = c;
  }
  if (a[i] && b[i]) {
    if (b[i] > a[i]) return dp[i][lg] = 0;
    if (a[i] > b[i]) return dp[i][lg] = state(i + 1, 1);
    return dp[i][lg] = state(i + 1, lg);
  }
  if (!a[i] && !b[i]) {
    long long w2 = m;
    w2 %= md;
    w2 *= state(i + 1, lg);
    w2 %= md;
    long long w1 = mm;
    w1 %= md;
    w1 *= state(i + 1, 1);
    w1 %= md;
    c += w1;
    c %= md;
    c += w2;
    c %= md;
    return dp[i][lg] = c;
  }
  if (a[i]) {
    long long w1 = 1;
    w1 *= (a[i] - 1);
    w1 %= md;
    w1 *= state(i + 1, 1);
    w1 %= md;
    c += w1;
    c %= md;
    c += state(i + 1, lg);
    c %= md;
    return dp[i][lg] = c;
  }
  if (b[i]) {
    long long w1 = 1;
    w1 *= (m - b[i]);
    w1 %= md;
    w1 *= state(i + 1, 1);
    w1 %= md;
    c += w1;
    c %= md;
    c += state(i + 1, lg);
    c %= md;
    return dp[i][lg] = c;
  }
}
long long pw(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long p1 = y / 2, p2 = y % 2;
  long long ans = pw(x, p1);
  ans *= ans;
  ans %= md;
  if (p2) ans *= x;
  ans %= md;
  return ans;
}
int main() {
  cin >> n >> m;
  mm = (m * (m - 1LL)) / 2LL;
  mm %= md;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  memset(dp, -1, sizeof dp);
  long long p = state(0, 0);
  long long q = 1;
  for (int i = 0; i < n; i++) {
    if (!a[i]) q *= m, q %= md;
    if (!b[i]) q *= m, q %= md;
  }
  long long ans = p;
  ans %= md;
  ans *= pw(q, md - 2);
  ans %= md;
  cout << ans << '\n';
  return 0;
}
