#include <bits/stdc++.h>
using namespace std;
const int mm = 1000000007;
const int maxn = 100009;
int n, m;
int s1[maxn];
int s2[maxn];
long long ans;
long long Ksm(long long a, int p) {
  long long ret = 1;
  for (; p; p >>= 1, a = a * a % mm) {
    if (p & 1) ret = ret * a % mm;
  }
  return ret;
}
long long Inv(long long x) { return Ksm(x, mm - 2); }
long long f[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &s1[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &s2[i]);
  f[0] = 1;
  for (int i = 1; i <= n; ++i) {
    if (s1[i] && s2[i]) {
      if (s1[i] != s2[i]) {
        f[i] = 0;
      } else {
        f[i] = f[i - 1];
      }
    } else {
      f[i] = f[i - 1] * Inv(m) % mm;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (s1[i] && s2[i]) {
      if (s1[i] > s2[i]) {
        ans = (ans + f[i - 1]) % mm;
      }
    }
    if (s1[i] && !s2[i]) {
      ans = (ans + f[i - 1] * (s1[i] - 1) % mm * Inv(m)) % mm;
    }
    if (!s1[i] && s2[i]) {
      ans = (ans + f[i - 1] * (m - s2[i]) % mm * Inv(m)) % mm;
    }
    if (!s1[i] && !s2[i]) {
      ans = (ans + f[i - 1] * (m - 1) % mm * Inv(2 * m)) % mm;
    }
  }
  cout << ans << endl;
  return 0;
}
