#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
constexpr int MOD = 998244353;
int sum(int a, int b, int mod = MOD) {
  int c = a + b;
  if (c >= mod) {
    c -= mod;
  }
  return c;
}
inline int prod(int a, int b, int mod = MOD) { return (1LL * a * b) % mod; }
int modpow(int base, ll exp, int mod = MOD) {
  int res = 1;
  int cur = base;
  for (ll p = 1LL; p <= exp; p <<= 1LL) {
    if (p & exp) {
      res = prod(res, cur, mod);
    }
    cur = prod(cur, cur, mod);
  }
  return res;
}
inline int inv(int x, int mod = MOD) { return modpow(x, mod - 2, mod); }
constexpr int MAXN = 1e6 + 6;
int fact[MAXN], tcaf[MAXN];
void gen_fact() {
  fact[0] = 1;
  for (int i = 1; i < MAXN; ++i) {
    fact[i] = prod(fact[i - 1], i);
  }
  tcaf[MAXN - 1] = inv(fact[MAXN - 1]);
  for (int i = MAXN - 2; i >= 0; --i) {
    tcaf[i] = prod(tcaf[i + 1], i + 1);
  }
}
int choose(int n, int k) {
  assert(n >= 0);
  assert(0 <= k and k <= n);
  return prod(fact[n], prod(tcaf[k], tcaf[n - k]));
}
int solve(ll n) {
  int ans = modpow(3, 1LL * n * n);
  for (int i = 1; i <= n; ++i) {
    int x = prod(choose(n, i), modpow(3, 1LL * (n * (n - i)) + i));
    int v = prod(2, x);
    if (i % 2 == 0) {
      ans = sum(ans, v);
    } else {
      ans = sum(ans, MOD - v);
    }
  }
  for (int i = 1; i < n; ++i) {
    int v = prod(6, choose(n, i));
    if ((i + n) % 2 == 0) {
      ans = sum(ans, v);
    } else {
      ans = sum(ans, MOD - v);
    }
  }
  ans = sum(ans, 3);
  int square_sum = 0;
  for (int i = 1; i < n; ++i) {
    int row_sum = modpow(1 + MOD - modpow(3, i), n);
    row_sum = sum(row_sum, MOD - 1);
    row_sum = sum(row_sum, MOD - modpow(MOD - modpow(3, i), n));
    square_sum =
        sum(square_sum, prod(modpow(MOD - 1, i), prod(choose(n, i), row_sum)));
  }
  ans = sum(ans, prod(3, square_sum));
  return sum(modpow(3, 1LL * n * n), MOD - ans);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  gen_fact();
  int n;
  cin >> n;
  cout << solve(n);
  return 0;
}
