#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int maxn = 1e5 + 5;
long long a[maxn], b[maxn], p[maxn << 1];
long long n, m, ans = 0, zero = 0;
long long f_pow(long long a, long long b) {
  long long ans = 1;
  long long base = a;
  while (b > 0) {
    if (b & 1) ans = ans * base % MOD;
    base = base * base % MOD;
    b >>= 1;
  }
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i], zero += (!a[i]);
  for (int i = 0; i < n; i++) cin >> b[i], zero += (!b[i]);
  p[0] = 1;
  for (int i = 1; i <= zero; i++) p[i] = (p[i - 1] * m) % MOD;
  long long dz = m * (m - 1) / 2 % MOD, npm = p[zero], base = 1;
  int cnt = 0, tot = zero;
  for (int i = 0; i < n; ++i) {
    cnt += !a[i];
    cnt += !b[i];
    if (!a[i] && !b[i]) {
      ans = (ans +
             base * p[tot - cnt] % MOD * (1ll * m * (m - 1) / 2 % MOD) % MOD) %
            MOD;
      base = base * m % MOD;
    } else if (!a[i]) {
      ans = (ans + base * p[tot - cnt] % MOD * (m - b[i]) % MOD) % MOD;
    } else if (!b[i]) {
      ans = (ans + base * p[tot - cnt] % MOD * (a[i] - 1) % MOD) % MOD;
    } else if (a[i] > b[i]) {
      ans = (ans + base * p[tot - cnt] % MOD) % MOD;
      break;
    } else if (a[i] < b[i]) {
      break;
    }
  }
  ans = (ans * f_pow(npm, MOD - 2)) % MOD;
  cout << ans << endl;
  return 0;
}
