#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const long long mod = 1e9 + 7;
long long p[N];
long long n, m;
long long t;
long long a[2][N];
long long powmod(long long a, long long b, long long mod) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long dfs(int i) {
  if (i > n) {
    return 0;
  }
  long long ans = 0;
  if (p[i] == -1)
    ans = 0;
  else if (p[i] == 2 * m)
    ans = p[i] * t % mod;
  else if (p[i] == -2)
    ans = dfs(i + 1) % mod;
  else
    ans = (p[i] % mod + 2 % mod * dfs(i + 1) % mod) * t % mod;
  return ans;
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (int j = 0; j < 2; j++) {
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[j][i]);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[0][i] && a[1][i]) {
      p[i] = (a[0][i] > a[1][i] ? 2 * m : -1);
      if (a[0][i] == a[1][i]) p[i] = -2;
    } else if (a[0][i] && !a[1][i])
      p[i] = 2 * a[0][i] - 2;
    else if (!a[0][i] && a[1][i])
      p[i] = 2 * m - 2 * a[1][i];
    else if (!a[0][i] && !a[1][i])
      p[i] = m - 1;
  }
  t = powmod(2 * m, mod - 2, mod);
  long long ans = dfs(1);
  printf("%lld\n", ans);
}
