720_D. Slalom

Little girl Masha likes winter sports, today she's planning to take part in slalom skiing.

The track is represented as a grid composed of n × m squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1, 1) to the square (n, m). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square.

One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way.

Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo 109 + 7.

The pictures below show different ways to pass the track in sample tests. <image> <image> <image>

Input

The first line of input data contains three positive integers: n, m and k (3 ≤ n, m ≤ 106, 0 ≤ k ≤ 105) — the size of the track and the number of obstacles.

The following k lines contain four positive integers each: x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — coordinates of bottom left, and top right squares of the obstacle. 

It is guaranteed that there are no obstacles at squares (1, 1) and (n, m), and no obstacles overlap (but some of them may touch).

Output

Output one integer — the number of ways to pass the track modulo 109 + 7.

Examples

Input

3 3 0


Output

1


Input

4 5 1
2 2 3 4


Output

2


Input

5 5 3
2 2 2 3
4 2 5 2
4 4 4 4


Output

3

Example input:
5 5 3
2 2 2 3
4 2 5 2
4 4 4 4

3 3 0

4 5 1
2 2 3 4


Example output:
3

1

2


Tags: data structures
dp
sortings
Difficulty rating: 3100

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005, P = 1e9 + 7;
void pp(int &a, int b) {
  a += b;
  if (a >= P) a -= P;
}
template <class T>
struct Fenwick {
  int n;
  T a[N];
  void ini(int _n) { fill_n(a + 1, n = _n, 0); }
  void add(int x, T d) {
    for (; x <= n; x += ((x) & -(x))) pp(a[x], d);
  }
  T sum(int x) {
    T r = 0;
    for (; x >= 1; x ^= ((x) & -(x))) pp(r, a[x]);
    return r;
  }
};
Fenwick<int> f;
vector<pair<int, int> > in[N], out[N];
set<pair<int, int> > inv;
set<int> pts;
int n, m, K;
int main() {
  scanf("%d%d%d", &n, &m, &K);
  f.ini(n);
  for (int i = 0; i < (K); ++i) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    in[y1].push_back(make_pair(x1, x2));
    out[y2 + 1].push_back(make_pair(x1, x2));
  }
  ++m;
  in[m].push_back(make_pair(1, n - 1));
  pts.insert(1);
  inv.insert(make_pair(0, 0));
  f.add(1, 1);
  for (int i = 1; i <= m; ++i)
    if ((int)(in[i]).size() || (int)(out[i]).size()) {
      sort((in[i]).begin(), (in[i]).end());
      reverse((in[i]).begin(), (in[i]).end());
      sort((out[i]).begin(), (out[i]).end());
      for (auto e : in[i]) {
        auto it = inv.upper_bound(make_pair(e.second + 1, n));
        --it;
        if (it->second <= e.second) {
          int s = (f.sum(e.second) - f.sum(it->second) + P) % P;
          f.add(e.second + 1, i > 1 ? s : 0);
          pts.insert(e.second + 1);
        }
      }
      for (auto e : in[i]) {
        auto it = pts.lower_bound(e.first);
        while (it != pts.end() && *it <= e.second) {
          int s = (-f.sum(*it) + f.sum(*it - 1) + P) % P;
          f.add(*it, s);
          pts.erase(it++);
        }
      }
      for (auto e : out[i]) inv.erase(e);
      for (auto e : in[i]) inv.insert(e);
    }
  printf("%d\n", f.sum(n));
  return 0;
}