Under construction

Still being built. Things may change

Buy Your House

Time limit1sMemory limit128 MB

Summary
For each of up to 500 tests with at most 50 non-overlapping axis-aligned houses, count integer-coordinate rectangles that contain exactly one house and split none, modulo 1e9+7.
Level

Hard8 of 10

Topics
Geometry, Combinatorics, Math, Brute force
Solved
No attempts yet

Problem

You want to buy a house, so you contact a real estate company that has just started its business — and you will be their very first customer. Because of that, they make you a special offer.

The company owns a single rectangular plot of land of width WW and height HH. Positions use a coordinate system whose origin (0,0)(0, 0) is the lower-left corner of the plot: a point that lies xx units to the right and yy units up from that corner is written as (x,y)(x, y), so every point of the plot satisfies 0≤x≤W0 \le x \le W and 0≤y≤H0 \le y \le H.

Several houses have already been built on the plot. Each house is an axis-aligned rectangle whose edges are parallel to the edges of the plot, and no two houses overlap. A house is described by four integers x1,y1,x2,y2x_1, y_1, x_2, y_2, where (x1,y1)(x_1, y_1) is its lower-left corner and (x2,y2)(x_2, y_2) its upper-right corner.

The special offer works like this: you may pick any axis-aligned rectangular region of the plot that contains exactly one house, together with as much (or as little) surrounding open space as you like. If you only want the land the house stands on, you may take exactly that; if you can afford more, you may keep some open space around it — a garden, for example.

The region you choose must obey these rules:

  • Its edges must be parallel to the edges of the plot, and all four corners must be at integer coordinates (for example (3,2)(3, 2) is allowed, but (3.5,2)(3.5, 2) is not).
  • No house may be split by the region's boundary: every house is either completely inside your region or completely outside it.
  • The region must contain exactly one house — never zero houses, and never two or more.

How many different regions can you choose?

Input

The first line contains an integer TT (about 500500), the number of test cases.

Each test case begins with a line containing two integers WW and HH (1≤W,H≤1091 \le W, H \le 10^9), the width and height of the plot. The next line contains an integer NN (1≤N≤501 \le N \le 50), the number of houses. Each of the following NN lines contains four integers x1 y1 x2 y2x_1\ y_1\ x_2\ y_2 (0≤x1<x2≤W0 \le x_1 < x_2 \le W and 0≤y1<y2≤H0 \le y_1 < y_2 \le H) describing one house. No two houses overlap, and all coordinates are non-negative integers.

Output

For each test case, print a single line in the form Case k: A, where k is the test case number (starting from 11) and A is the number of regions you can choose. Because this number can be very large, print it modulo 109+710^9 + 7.

Examples3

  1. Example 1

    Input
    2
    3 3
    1
    1 1 2 2
    10 10
    2
    1 1 4 4
    6 6 8 8
    
    Expected output
    Case 1: 16
    Case 2: 429
    
  2. Example 2

    Input
    1
    1 1
    1
    0 0 1 1
    
    Expected output
    Case 1: 1
    
  3. Example 3

    Input
    1
    5 5
    1
    2 2 3 3
    
    Expected output
    Case 1: 81