#include <bits/stdc++.h>
using namespace std;
int n;
long long m;
long long dp[2][1234567], a[2][1234567];
long long GCD(long long x, long long y) {
  if (x % y == 0) return y;
  return GCD(y, x % y);
}
long long mExp(long long x, long long n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return mExp((x * x) % 1000000007, n / 2);
  else
    return (x * mExp((x * x) % 1000000007, (n - 1) / 2)) % 1000000007;
}
long long modInverse(long long A, long long eM) { return mExp(A, eM - 2); }
long long kgv(long long x, long long y) {
  long long g = GCD(x, y);
  return (x * y) / g;
}
void addi(int i) {
  long long tmp = (dp[1][i + 1] * m) % 1000000007;
  long long k = kgv(dp[1][i], tmp);
  dp[0][i] = (((k / dp[1][i]) * dp[0][i]) % 1000000007) +
             (((k / tmp) * dp[0][i + 1]) % 1000000007);
  dp[1][i] = k % 1000000007;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) scanf("%I64d", &a[0][i]);
  for (int i = 0; i < n; i++) scanf("%I64d", &a[1][i]);
  for (int i = n - 1; i >= 0; i--) {
    if (a[0][i] == 0 && a[1][i] == 0) {
      dp[0][i] = m - 1;
      dp[1][i] = 2 * m;
      if (dp[0][i + 1] > 0) addi(i);
    } else if (a[0][i] == 0) {
      dp[0][i] = m - a[1][i];
      dp[1][i] = m;
      if (dp[0][i + 1] > 0) addi(i);
    } else if (a[1][i] == 0) {
      dp[0][i] = a[0][i] - 1;
      dp[1][i] = m;
      if (dp[0][i + 1] > 0) addi(i);
    } else {
      if (a[0][i] > a[1][i]) {
        dp[0][i] = 1;
        dp[1][i] = 1;
      } else if (a[0][i] < a[1][i]) {
        dp[0][i] = 0;
        dp[1][i] = 0;
      } else {
        dp[0][i] = dp[0][i + 1];
        dp[1][i] = dp[1][i + 1];
      }
    }
  }
  if (dp[0][0] == 0) {
    cout << 0 << endl;
  } else {
    long long ggt = GCD(dp[0][0], dp[1][0]);
    dp[0][0] /= ggt;
    dp[1][0] /= ggt;
    cout << (dp[0][0] * modInverse(dp[1][0], 1000000007)) % 1000000007 << endl;
  }
  return 0;
}
