Problem:
Problem D: Gridgedge

Problem

左上が$(0, 0)$、 右下が$(R-1, C-1)$である$R \times C$マスのグリッドがある。あるマス($e$, $f$)にいるとき、そこから$(e+1, f)$, $(e-1, f)$, $(e, f+1)$, $(e, f-1)$, $(e, 0)$, $(e, C-1)$, $(0, f)$, $(R-1, f)$にはコスト$1$で移動できる。ただしグリッドの外に出ることはできない。マス$(a_i, a_j)$から$(b_i, b_j)$へ移動するとき、最短経路のコストと、最短経路の組み合わせの総数を$10^9 + 7$で割った余りを計算せよ。

ただし、経路の組み合わせは移動の方法によって区別するものとする。たとえば、現在地が$(100, 1)$ のとき、 $(100, 0)$に最短コストで行くには上記$(e, f-1)$か$(e, 0)$で行くことができるので、$2$通りと数える。

Input

入力は以下の形式で与えられる。

$R$ $C$ $a_i$ $a_j$ $b_i$ $b_j$

Constraints

入力は以下の条件を満たす。

$1 \le R, C \le 500$

$0 \le a_i, b_i \le R - 1$

$0 \le a_j, b_j \le C - 1$

与えられる入力は全て整数である。

Output

マス$(a_i, a_j)$から$(b_i, b_j)$へ移動するとき、最短経路のコストと、最短経路の組み合わせの総数を$10^9+7$で割った余りを空白区切りで$1$行に出力せよ。

Sample Input 1

2 2 0 0 1 1

Sample Output 1

2 8

Sample Input 2

1 1 0 0 0 0

Sample Output 2

0 1

Sample Input 3

1 10 0 0 0 9

Sample Output 3

1 1

Sample Input 4

5 5 0 0 4 4

Sample Output 4

2 2

Sample Input 5

421 435 196 169 388 9

Sample Output 5

43 917334776

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using static Template;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pi = Pair<int, int>;
using Number = System.Int64;

class Solver
{
    Scanner sc = new Scanner();
    //

    public void Solve()
    {
        Grid_bfs(sc.Next<int>(), sc.Next<int>(), sc.PairMake<int, int>(), sc.PairMake<int, int>());

    }


    public int[][] Grid_bfs(int h,int w,Pi st, Pi g)
    {
        var dist = Create(h, () => Create(w, () => -1));
        var que = new Queue<Pi>();
        var dp = Create(h, () => new ModInt[w]);
        dp[st.v1][st.v2] = 1;
        dist[st.v1][st.v2] = 0;
        que.Enqueue(st);

        while (que.Any())
        {
            var p = que.Dequeue();
            if (p.CompareTo(g)==0)
                Fail($"{dist[p.v1][p.v2]} {dp[p.v1][p.v2]}");
            var next = new List<Pi>();
            for (var i = 0; i < 4; i++) next.Add(new Pi(gh[i] + p.v1, gw[i] + p.v2));
            next.Add(new Pi(p.v1, 0));
            next.Add(new Pi(p.v1, w - 1));
            next.Add(new Pi(0, p.v2));
            next.Add(new Pi(h - 1, p.v2));
            foreach (var n in next)
            {
                int nh, nw;
                n.Deconstruct(out nh, out nw);
                if (!Inside(nh, nw, h, w)) continue;
                if (dist[nh][nw] == -1)
                {
                    que.Enqueue(new Pi(nh, nw));
                    dist[nh][nw] = dist[p.v1][p.v2] + 1;
                }
                if (dist[nh][nw] == dist[p.v1][p.v2] + 1)
                    dp[nh][nw] += dp[p.v1][p.v2];
                
            }
        }
        return dist;
    }

    int[] gh = new[] { 0, 0, 1, -1 }, gw = new[] { 1, -1, 0, 0 };
    public static bool Inside(int h, int w, int hh, int ww)
        => 0 <= h && h < hh && 0 <= w && w < ww;
}

public struct ModInt
{
    public const long MOD = (int)1e9 + 7;
    //public const long MOD = 998244353;
    public long num { get; set; }
    public ModInt(long n = 0) { num = n; }
    private static ModInt[] _fac;//階乗
    private static ModInt[] _inv;//逆数
    private static ModInt[] _facrev;//1/(i!)
    public override string ToString()
        => num.ToString();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator +(ModInt l, ModInt r)
    {
        l.num += r.num;
        if (l.num >= MOD) l.num -= MOD;
        return l;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator -(ModInt l, ModInt r)
    {
        l.num -= r.num;
        if (l.num < 0) l.num += MOD;
        return l;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator *(ModInt l, ModInt r)
        => new ModInt(l.num * r.num % MOD);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt operator /(ModInt l, ModInt r)
        => l * Pow(r, MOD - 2);
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator long(ModInt l)
        => l.num;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static implicit operator ModInt(long n)
    {
        n %= MOD; if (n < 0) n += MOD;
        return new ModInt(n);
    }

    public static ModInt Pow(ModInt m, long n)
    {
        if (n == 0) return 1;
        if (n % 2 == 0) return Pow(m * m, n >> 1);
        else return Pow(m * m, n >> 1) * m;
    }

    public static void CombBuild(int n)
    {
        _fac = new ModInt[n + 1];
        _facrev = new ModInt[n + 1];
        _inv = new ModInt[n + 1];
        _inv[1] = 1;
        _fac[0] = _fac[1] = 1;
        _facrev[0] = _facrev[1] = 1;
        for (var i = 2; i <= n; i++)
        {
            _fac[i] = _fac[i - 1] * i;
            _inv[i] = MOD - _inv[MOD % i] * (MOD / i);
            _facrev[i] = _facrev[i - 1] * _inv[i];
        }
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Fac(ModInt n)
        => _fac[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Div(ModInt n)
        => _inv[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt FacRev(ModInt n)
        => _facrev[n];
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ModInt Comb(ModInt n, ModInt r)
    {
        if (n < r) return 0;
        if (n == r) return 1;
        var calc = _fac[n];
        calc = calc * _facrev[r];
        calc = calc * _facrev[n - r];
        return calc;
    }
}
#region Template
public class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver().Solve();
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == 1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == -1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T v1, ref T v2)
    { var t = v2; v2 = v1; v1 = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<T> f)
    {
        var rt = new T[n];
        for (var i = 0; i < rt.Length; ++i)
            rt[i] = f();
        return rt;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<int, T> f)
    {
        var rt = new T[n];
        for (var i = 0; i < rt.Length; ++i)
            rt[i] = f(i);
        return rt;
    }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
}

public class Scanner
{
    StreamReader sr;
    public Scanner() { sr = new StreamReader(Console.OpenStandardInput()); }
    public Scanner(string path) { sr = new StreamReader(path); }
    public string Str => sr.ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    public Pair<T1, T2> PairMake<T1, T2>() => new Pair<T1, T2>(Next<T1>(), Next<T2>());
    public Pair<T1, T2, T3> PairMake<T1, T2, T3>() => new Pair<T1, T2, T3>(Next<T1>(), Next<T2>(), Next<T3>());
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>()
    {
        if (q.Count == 0)
            foreach (var item in Str.Split(' ')) q.Enqueue(item);
        return (T)Convert.ChangeType(q.Dequeue(), typeof(T));
    }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2)
    {
        v1 = Next<T1>();
        v2 = Next<T2>();
    }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3)
    {
        Make(out v1, out v2);
        v3 = Next<T3>();
    }
    public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4)
    {
        Make(out v1, out v2, out v3);
        v4 = Next<T4>();
    }
    public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5)
    {
        Make(out v1, out v2, out v3, out v4);
        v5 = Next<T5>();
    }
    public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6)
    {
        Make(out v1, out v2, out v3, out v4, out v5);
        v6 = Next<T6>();
    }
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
    public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; }
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
    public void Deconstruct(out T1 a, out T2 b,out T3 c) { Deconstruct(out a, out b);c = v3; }
}
#endregion