#include <bits/stdc++.h>
using namespace std;
long long ans, a[1001000], b[1001000], n, m;
pair<long long, long long> dp[1001000];
long long sum(long long a, long long b) { return (a + b) % 1000000007; }
long long sub(long long a, long long b) {
  return (((a - b) % 1000000007) + 1000000007) % 1000000007;
}
long long mul(long long a, long long b) {
  return ((a % 1000000007) * (b % 1000000007)) % 1000000007;
}
long long expo(long long a, long long b) {
  if (!b) return 1;
  a %= 1000000007;
  if (b % 2) return mul(a, expo(a * a, b >> 1));
  return expo(a * a, b >> 1);
}
long long inv(long long a) { return expo(a, 1000000007 - 2); }
long long divi(long long a, long long b) { return mul(a, inv(b)); }
pair<long long, long long> sum(pair<long long, long long> a,
                               pair<long long, long long> b) {
  long long dn = (a.second * b.second) % 1000000007;
  long long nm = (a.first * b.second + b.first * a.second) % 1000000007;
  return {nm, dn};
}
pair<long long, long long> sub(pair<long long, long long> a,
                               pair<long long, long long> b) {
  long long dn = (a.second * b.second) % 1000000007;
  long long nm =
      ((a.first * b.second - b.first * a.second) % 1000000007 + 1000000007) %
      1000000007;
  return {nm, dn};
}
pair<long long, long long> mul(pair<long long, long long> a,
                               pair<long long, long long> b) {
  long long dn = (a.second * b.second) % 1000000007;
  long long nm = (a.first * b.first) % 1000000007;
  return {nm, dn};
}
int main() {
  scanf("%lld %lld", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &b[i]);
  }
  dp[n] = {0, 1};
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] && b[i]) {
      if (a[i] > b[i])
        dp[i] = {1, 1};
      else if (a[i] < b[i])
        dp[i] = {0, 1};
      else
        dp[i] = dp[i + 1];
    } else if (a[i]) {
      dp[i] = sum({a[i] - 1, m}, mul({1, m}, dp[i + 1]));
    } else if (b[i]) {
      dp[i] = sum({m - b[i], m}, mul({1, m}, dp[i + 1]));
    } else {
      long long M = mul(m, m);
      long long W = divi(sub(M, m), 2);
      dp[i] = sum({W, M}, mul({m, M}, dp[i + 1]));
    }
  }
  printf("%lld\n", divi(dp[0].first, dp[0].second));
  return 0;
}
