#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y, z;
  bool operator<(const P &a) const { return x > a.x; }
};
vector<P> v, v1;
int a, c, i, b, k, n, d, m;
long long o[1000044];
long long l[1005433];
int j[113];
int e;
int dx[10] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[10] = {0, 1, 0, -1, 1, -1, 1, -1},
    dz[10] = {0, 0, 0, 0, 1, -1};
long long x, y, mod = 998244353;
long long z;
P u[100403];
stack<int> s;
queue<P> q, q1;
bool as(P a, P b) { return a.x < b.x; }
bool asd(P a, P b) { return a.y < b.y; }
long long f(long long a, long long b) {
  a %= mod;
  long long u = 1;
  for (; b;) {
    if (b & 1) u *= a, u %= mod;
    a *= a;
    a %= mod;
    b /= 2;
  }
  return u;
}
int main() {
  scanf("%d", &a);
  o[0] = 1;
  l[0] = 1;
  for (int t = 1; t <= a; t++) {
    o[t] = o[t - 1] * t % mod;
    l[t] = f(o[t], mod - 2);
  }
  x = f(3, (long long)a * a);
  x -= f(f(3, a) - 3 + mod, a);
  for (int t = 1; t <= a; t++) {
    if (t % 2)
      i = 1;
    else
      i = -1;
    x += i * o[a] * l[a - t] % mod * l[t] % mod *
         (f(f(3, a - t) - 1 + mod, a) * 3 % mod +
          (f(3, t) - 3 + mod) * f(3, (long long)a * a - (long long)a * t) %
              mod) %
         mod;
    x %= mod;
    x += mod;
    x %= mod;
  }
  printf("%lld", x);
}
