#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
int n, m, s1[MAXN], s2[MAXN];
long long int minv, minv2, tw, dp[MAXN];
long long int bp(long long int x, long long int p) {
  if (!p) return 1;
  long long int tmp = bp(x, p / 2);
  tmp = (tmp * tmp) % MOD;
  return (p & 1) ? ((x * tmp) % MOD) : tmp;
}
long long int solve(int i) {
  if (i == n) return 0;
  if (dp[i] >= 0) return dp[i];
  long long int ans = 0;
  if (s1[i] && s2[i]) {
    ans = (s1[i] == s2[i]) ? solve(i + 1) : (s1[i] > s2[i]);
  } else if (s1[i]) {
    long long int left = s1[i] - (long long int)1ll;
    long long int pg = (left * minv) % MOD;
    long long int ps = (solve(i + 1) * minv) % MOD;
    ans = (pg + ps) % MOD;
  } else if (s2[i]) {
    long long int left = m - (long long int)s2[i];
    long long int pg = (left * minv) % MOD;
    long long int ps = (solve(i + 1) * minv) % MOD;
    ans = (pg + ps) % MOD;
  } else {
    long long int pg = ((m - 1) * minv2) % MOD;
    long long int ps = (solve(i + 1) * minv) % MOD;
    ans = (pg + ps) % MOD;
  }
  return dp[i] = ((ans + MOD) % MOD);
}
int main() {
  scanf("%d%d", &n, &m);
  minv = bp(m, MOD - 2);
  minv2 = bp(2 * m, MOD - 2);
  tw = bp(2, MOD - 2);
  for (int i = 0; i < n; ++i) scanf("%d", s1 + i);
  for (int i = 0; i < n; ++i) {
    scanf("%d", s2 + i);
    dp[i] = -1;
  }
  printf("%lld\n", solve(0));
  return 0;
}
